Basics Flashcards

1
Q

Adding WebDriver to classpath in Windows

A

setx /m path “%path%;C:\selenium-drivers\bin”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Adding WebDriver to classpath in Linux

A

sudo mkdir -p $HOME/selenium-drivers

export PATH=$PATH:$HOME/selenium-drivers&raquo_space; ~/.profile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are Desired Capabilities in Browser Automation?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can I create an instance of ChromeOptions with Chrome-specific capabilities?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is mobile emulation in Chrome?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do we set desired capabilities for mobile emulation in WebDriver?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we add custom devices for mobile emulation?

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Expected Condition associated with Alerts management in Selenium?

A

ExpectedConditions.alertIsPresent()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Implement JavascriptExecutor:

A

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()");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Verify the printed option == empty

A

WebElement result = driver.findElement(By.id(“demo1”));

Assert.assertTrue(result.getText().trim().isEmpty());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

// Assert that a checkbox it’s already selected

A

Assert.assertFalse(checked.isSelected());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an HTTP cookie?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Selenium WebDriver interaction with cookies

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

GET cookie

A
//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();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

ADD Cookie

A
// adds a cookie in Selenium WebDriver
Cookie addCookie = new Cookie("newCookie", "cookieValue");
driver.manage().addCookie(addCookie);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

DELETE Cookie

A
//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();
17
Q

org.openqa.selenium.interactions.Actions

A

contains methods to build complex user gestures that involve Keyboard and Mouse interactions.

18
Q

Drag & Drop

A
// 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();
19
Q

Mouse hover

A

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();
20
Q

Double click

A
// 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();
21
Q

Right Click

A
WebElement source = driver.findElement(By.id("prompt"));
		Actions actions = new Actions(driver);
	actions.contextClick(source).build().perform();
22
Q

Take Screenshot

A

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