This post will describe some of the basic webdriver commands we as testers use daily to perform actions on web applications. I call this my personal Selenium webdriver handbook, because i can always refer to commands i may have forgotten while writing my test.
Over the years i have come across different commands and best practices with Selenium, which makes me refactor most of my code from time to time. With this in mind, I will keep updating this post from time to time, as i explore or come across better practices.
The focus language here is Java.
- Create a new instance of Firefox Driver
WebDriver driver = new FirefoxDriver();
- Create a new instance of Chrome Driver
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
WebDriver driver = new ChromeDriver(options);
- Create a new instance of IE Driver
System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
// to resolve "enable protected Mode for all zones" Error RunTime
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(cap);
- load Url
driver.get("http://www.testdiary.com/");
- Get page title
driver.getTitle();
- Get current URL
driver.getCurrentUrl();
- Nagivate to URL, Navigate Back, Navigate Forward
driver.navigate().to("http://www.testdiary.com/training/seleniumbeginner/");
//navigate to previous page
driver.navigate().back();
//navigate forward
driver.navigate().forward();
- Click on an element in on a webpage
driver.findElement(By.id("xxxxxx")).click();
- Insert text into a text field
driver.findElement(By.name("xxxx")).sendKeys("Olufemi Ade-olusile");
- Get text from webpage
String housenames = driver.findElement(By.cssSelector("selector").getText();
- Handling drop downs (Select and deselect by Visible Text)
//select
Select country = new Select(driver.findElement(By.id("xxxx")));
country.selectByVisibleText("United Kingdom");
//Deselect
Select country = new Select(driver.findElement(By.id("xxxx")));
country.deselectByVisibleText("United Kingdom");
- Handling drop downs (Select and deselect by Visible Value)
//Select
Select country = new Select(driver.findElement(By.name("xxxxx")));
country.selectByValue("UK");
//Deselect
Select country = new Select(driver.findElement(By.name("xxxxx")));
country.deselectByValue("UK");;
- Handling drop downs (Select and deselect by Visible Index)
//Select
Select country = new Select(driver.findElement(By.xpath("xxxxx")));
country.selectByIndex(40);
//Deselect
Select country = new Select(driver.findElement(By.xpath("xxxxx")));
country.deselectByIndex(40);
- Handling drop downs (deselect all)
Select country = new Select(driver.findElement(By.id("xxxxx")));
country.deselectAll();
- Implicit wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
- Explicit wait
WebDriverWait pause = new WebDriverWait(driver, 20);
pause.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xxxx")));
- Return Domain name with Javascript
JavascriptExecutor domainname = (JavascriptExecutor) driver;
String currDomainName =(String)domainname.executeScript("return document.domain");
- Generate alert popup window in Selenium (Javascript)
JavascriptExecutor Jalert = (JavascriptExecutor)driver;
Js.executeScript("alert('Testdiary is good');");
- Click a button with Javascript
JavascriptExecutor click = (JavascriptExecutor)driver;
click.executeScript("arguments[0].click();", xxxxxx);
- Verify if element is present on webpage
Boolean iselementpresent = driver.findElements(By.id("xxxx")).size()!= 0;
- Enable or Disable Text box with Javascript
// enable textbox
JavascriptExecutor js = (JavascriptExecutor) driver;
String enable = "document.getElementsByid('xxxxx')[0].removeAttribute('disabled');";
js.executeScript(enable);
// disable textbox
JavascriptExecutor js = (JavascriptExecutor) driver;
String disable = "document.getElementsByid('xxxxx')[0].setAttribute('disabled', '');";
js.executeScript(disable);
- Check if element is Enabled, is selected, is displayed
// is enabled
boolean test = driver.findElement(By.xpath("xxx")).isEnabled();
System.out.print(test);
// is selected
boolean test = driver.findElement(By.xpath("xxx")).isSelected();
System.out.print(test);
// is displayed
boolean test = driver.findElement(By.xpath("xxx")).isDisplayed();
System.out.print(test);
- Capture full page screenshot
File newscreenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(newscreenshot, new File("C:\\newscreenshot.jpg"));
- Handle multiple windows
//Get All Window Handles.
Set<String> window = driver.getWindowHandles();
//handle the parent and child window from all window handles
String firstwindow = (String) window.toArray()[0];
String secondwindow = (String) window.toArray()[1];
//switch from one window to another
driver.switchTo().window(secondwindow);
driver.switchTo().window(firstwindow);
- Accept page alert
driver.switchTo().alert().accept();
- Insert text into alert text box
driver.switchTo().alert().sendKeys("testdiary is good");
- Dismiss alert text box
driver.switchTo().alert().dismiss();
- Mouse hover
Actions mouse = new Actions(driver);
WebElement move = driver.findElement(By.name("xxxxx"));
mouse.moveToElement(move);
- Tables and Selenium
WebElement table = driver.findElement(By.xpath("xxxxx"));
//save table row as a list
List<WebElement> row = table.findElements(By.tagName("tr"));
//iterate through the table row
for (WebElement rnum : row) {
//Save column with tagname td on every row as a list
List<WebElement> col = rnum.findElements(By.tagName("td"));
//get column size
col.size();
//click on column 0 on every row
col.get(0).findElement(By.tagName("a")).click();
}
Next post will begin our first test with Selenium Webdriver
Happy Testing!!!!!
Recent Comments