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);