Basics Flashcards
Adding WebDriver to classpath in Windows
setx /m path “%path%;C:\selenium-drivers\bin”
Adding WebDriver to classpath in Linux
sudo mkdir -p $HOME/selenium-drivers
export PATH=$PATH:$HOME/selenium-drivers»_space; ~/.profile
What are Desired Capabilities in Browser Automation?
A collection of key/value pairs used to configure a particular instance of the browser. It helps to set the properties or capabilities of a WebDriver. There are browser-independent capabilities like browser name, version, acceptSslCerts, etc and browser-specific capabilities like FirefoxOptions for Mozilla Firefox, and ChromeOptions for Chrome.
How can I create an instance of ChromeOptions with Chrome-specific capabilities?
ChromeOptions options = new ChromeOptions();
options. addExtensions(new File(“”)); // add an extension
options. setAcceptInsecureCerts(true); // accept all SSL certs by defaults
options. setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
options. setHeadless(true); // To run Chrome browser in a headless environment
options. addArguments(“start-maximized”); // Start Chrome maximized
WebDriver driver = new ChromeDriver(options);
What is mobile emulation in Chrome?
Chrome allows users to emulate Chrome on a mobile device (e.g. an iPad, or an iPhone X or a Pixel 2, etc.) from the desktop version of Chrome; enable this using Chrome DevTools.
How do we set desired capabilities for mobile emulation in WebDriver?
Map mobileEmulation = new HashMap<>(); // adding name of the device to emulate mobileEmulation.put("deviceName", "iPhone X");
ChromeOptions chromeOptions = new ChromeOptions(); // Experimental Option needs to be set for emulation chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation); WebDriver driver = new ChromeDriver(chromeOptions);
How can we add custom devices for mobile emulation?
Map deviceMetrics = new HashMap<>();
// device with custom width, height and pixel ratio
deviceMetrics.put(“width”, 380);
deviceMetrics.put(“height”, 670);
deviceMetrics.put(“pixelRatio”, 3.0);
Map mobileEmulation = new HashMap<>();
mobileEmulation.put(“deviceMetrics”, deviceMetrics);
// device having custom user agent
mobileEmulation.put(“userAgent”: “Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome / 66.0.3329.0 Mobile Safari / 537.36”);
ChromeOptions chromeOptions = new ChromeOptions();
// ExperimentalOption needs to be set for emulation chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation); WebDriver driver = new ChromeDriver(chromeOptions);
Expected Condition associated with Alerts management in Selenium?
ExpectedConditions.alertIsPresent()
Implement JavascriptExecutor:
import org.openqa.selenium.JavascriptExecutor;
// Create a Javascript executor object. JavascriptExecutor jsDriver = (JavascriptExecutor) driver; // Scroll down to the element jsDriver.executeScript("document.getElementById('my-select').scrollIntoViewIfNeeded()");
Verify the printed option == empty
WebElement result = driver.findElement(By.id(“demo1”));
Assert.assertTrue(result.getText().trim().isEmpty());
// Assert that a checkbox it’s already selected
Assert.assertFalse(checked.isSelected());
What is an HTTP cookie?
The web browser of the client machine stores information received from a website as a key-value pair when the user does web browsing and puts it in a text file. When the user visits that website again, the browser sends back the stored cookie to the server and notifies the user’s stored information and past activities.
Selenium WebDriver interaction with cookies
Selenium WebDriver provides org.openqa.selenium.WebDriver.Options class that contains the following built-in methods to interact with the browser cookies:
Get cookies Add cookies Delete cookies
GET cookie
//gets a cookie with a given name String getCookieValue = driver.manage().getCookieNamed(cookieName).getValue(); //gets all the cookies for the current domain Set getAllCookies = driver.manage().getCookies();
ADD Cookie
// adds a cookie in Selenium WebDriver Cookie addCookie = new Cookie("newCookie", "cookieValue"); driver.manage().addCookie(addCookie);
DELETE Cookie
//deletes a cookie from the browser’s “cookie jar driver.manage().deleteCookie(cookie); // deletes the named cookie from the current domain driver.manage().deleteCookieNamed(cookieName); // deletes all the cookies for the current domain driver.manage().deleteAllCookies();
org.openqa.selenium.interactions.Actions
contains methods to build complex user gestures that involve Keyboard and Mouse interactions.
Drag & Drop
// scroll to the end of the page JavascriptExecutor jsDriver = (JavascriptExecutor) driver; jsDriver.executeScript("document.getElementById('div1').scrollIntoViewIfNeeded()");
WebElement source = driver.findElement(By.id("drag1")); WebElement target = driver.findElement(By.id("div1")); // using Robot, move the mouse pointer to the target element // This is required, as the drop happens onto the location where the move // pointer is currently placed Robot robot = new Robot(); // get co-ordinates to the center of the target element Long height = (Long) jsDriver.executeScript("return window.innerHeight");
int x = target.getLocation().getX() + target.getRect().getWidth() / 2; int y = target.getLocation().getY() + target.getRect().getHeight() - height.intValue(); // move the mouse pointer to the co-ordinates robot.mouseMove(x, y);
// perform drag-drop Actions actions = new Actions(driver); // drag and drop by offset actions.dragAndDropBy(source, 100, 30).build().perform();
Mouse hover
JavascriptExecutor jsDriver = (JavascriptExecutor) driver;
// Scroll down to the bottom of the page jsDriver.executeScript("document.getElementById('mouseover').scrollIntoViewIfNeeded()");
WebElement source = driver.findElement(By.id("mouseover")); Actions action = new Actions(driver); action.moveToElement(source).build().perform();
Double click
// scroll to the end of the page JavascriptExecutor jsDriver = (JavascriptExecutor) driver; jsDriver.executeScript("document.getElementById('dbl-click').scrollIntoViewIfNeeded()");
// Find the element and perform an operation WebElement source = driver.findElement(By.id("dbl-click"));
Actions actions = new Actions(driver); actions.doubleClick(source).build().perform();
Right Click
WebElement source = driver.findElement(By.id("prompt")); Actions actions = new Actions(driver);
actions.contextClick(source).build().perform();
Take Screenshot
// take screenshot of the whole window
File source = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File destination = new File(“screenshot.png”);
source.renameTo(destination);
// take screenshot of the WebElement WebElement element = driver.findElement(By.id("drag1")); File source = element.getScreenshotAs(OutputType.FILE); File destination = new File("screenshot.png"); source.renameTo(destination);