Selenium for everyone.

Explore and practice, make mistakes and fix them, learn a few techniques, have fun.

Why Selenium

- It's an open source tool that is supported by a great community.

- It allows QA engineers to write/run test cases in Java, C#, Python, JavaScript, Ruby, and a few more...

- It makes it easy to implement test cases, just need to know how to locate a web element in the browser and perform actions.

A web element is ...

anything you can find on a web page: text boxes, checkboxes, radio buttons, dropdowns, links, etc. Each element has a unique identifier, such as ID, name or unique classes. A web element can be located by ID, XPath or CSS Selectors.

Explore web elements and a little extra

Click each element to learn how to perform action using Selenium WebDriver Java API

A few tips & tricks

JavaScript to set attribute

WebElement element = driver.findElement(By.cssSelector(label));

String js = "arguments[0].setAttribute('value','"+text+"')";

((JavascriptExecutor) driver).executeScript(js, element);

Navigate to a new window and back to the parent window

public String switchToNewWindow(WebDriver driver)

{

String parentWind = driver.getWindowHandle();

for (String childWin : driver.getWindowHandles()) {

driver.switchTo().window(childWin);

}

return parentWind;

}

public void switchToParentWindow(WebDriver driver, String parentWindow)

{

driver.close();

driver.switchTo().window(parentWindow);

}

Navigate to a new iframe

public void switchToIFrame(WebDriver driver, String iframeId)

{

WebElement iframeSwitch = driver.findElement(By.id(iframeId));

driver.switchTo().frame(iframeSwitch);

}

public void switchToParentWindow(WebDriver driver)

{

driver.switchTo().defaultContent();

}

Scroll on a web page to a specific web element

JavascriptExecutor executor = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("xpath"));
executor.executeScript("arguments[0].scrollIntoView();", element);

Take a screenshot

TakesScreenshot image =(TakesScreenshot) driver;
File srcfile=image.getScreenshotAs(OutputType.FILE);
File desfile=new File("file-with-path");
FileUtils.copyFile(srcfile, desfile);

Submit with alert

WebElement webel = driver.findElement(By.xpath("//*[@id=\"submit_it\"]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", webel);
wait.until(ExpectedConditions.alertIsPresent());
String alertText = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();

Recorded web test automation

© CollColl-2020
Java source code
Practice more