Home Commands Tips To Use In Selenium

Commands Tips To Use In Selenium

Commands:- Commands are keys to drive test script and with commands can give actions for test script execution, means there is need to know about commands.

  1. Hit url

driver.get(“qatestingtips.com”);

  1. Find elements

driver.findElement(By.id(“give id’s value as previously learned in post What is Locator in Selenium and How to Use?”);

For example we used “search related element”

driver.findElement(By.id(“s”));

Note: If using different locator then give value of other locator.

  1. Click element

driver.findElement(By.id(“s”).click();

  1. Send values for any text/input box

driver.findElement(By.id(“s”).sendKeys(“what is locator”));

  1. Select value from drop-down

Select category = new Select (driver.findElement(By.id(“.ui-datepicker-month”)));

month.selectByValue(“3”);

  1. Select value by link text

driver.findElement(By.linkText(“25”)).click();

  1. Hover Command

import org.openqa.selenium.interactions.Actions;  //need to add statement

import org.openqa.selenium.WebElement;  //need to add statement

Actions builder = new Actions(driver);

WebElement element = driver.findElement(By.linkText(“Put your text here”));

builder.moveToElement(element).build().perform();

Alert Handling – Ok and Cancel Buttons

  1. Click on the ‘Cancel’ button of the alert.

driver.switchTo().alert().dismiss();

  1. Click on the ‘OK’ button of the alert.

driver.switchTo().alert().accept();

Page Scroll code:- Using Javascript

First way:-

import org.openqa.selenium.JavascriptExecutor;  (Import)

((JavascriptExecutor)driver).executeScript(“scroll(0,400)”);  (Write code)

Second way:-

import org.openqa.selenium.JavascriptExecutor;  (Import)

JavascriptExecutor js = ((JavascriptExecutor) driver);

js.executeScript(“window.scrollTo(0, document.body.scrollHeight)”);