Practical Code Flashcards

1
Q

what is the recommended way to install a browser driver in order to use it in Selenium?

A
  • to download the Chrome driver (conveniently named chromedriver.exe)
  • install (store) that file in a well-known place (directory) on the test workstation.
  • add directory to the Path environmental variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What happens when we create a WebDriver object?

A
  • An API between a script and the web browser is created

- An instance of the web browser is created

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

Why is Web Driver protocol enabled to run several tests simultaneously or to control several browsers in one script?

A

Because WebDriver protocol is based on HTTP communication

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

How do we create a WebDriver object to interact with Chrome Browser?

A

from selenium import webdriver

driver = webdriver.Chrome()

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

CODE: To navigate to desired site page

A

driver.get(‘https://www.python.org’)

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

CODE: Two useful attributes that the WebDriver object contains to check if the correct page has
been opened:

A
  • driver.current_url

- driver.title

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

CODE: To close the browser controlled by webdriver

A

driver.quit()

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

CODE: To close an specific tab in the controlled web browser instance

A

driver.close()

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

What is the safest way to determine which window is the currently open window?

A

driver.title attribute

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

CODE: To simulate navigating forward, backward and refresh the web browser

A
  • driver.back()
  • driver.forward()
  • driver.refresh()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

three ways in which a script context can be changed:

A
  • changing browsers
  • changing windows/tabs within one browser
  • changing frames within one page
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

where the list of all open tabs (windows) is stored?

A

An array that can be accessed through the window_handles attribute of the WebDriver object.

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

CODE: The way to switch between open tabs in a browser (using Windows)

A

for handle in driver.window_handles:

driver.switch_to.window(handle)

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

CODE: One way to Open up multiple tabs in Chrome browser (using Windows):

A

driver.execute_script( “$(window.open(‘’)” )

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

What is necessary to be able to find elements in a specific frame?

A

change the context to that particular frame

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

CODE: To change context to a specific frame.

A

driver.switch_to.frame(‘frame_ID’)

17
Q

CODE: What happens if the frame has no ID or we want to use different strategy to find it?

A

frame_location = driver.find_element_by_name(‘frame_name’)

driver.switch_to.frame( frame_location )

18
Q

CODE: to switch back to the parent frame

A

driver.switch_to.parent_frame()

19
Q

CODE: switch back to the whole page

A

driver.switch_to.default_content()

20
Q

CODE: Three ways to manipulate the size of a web browser’s window using Selenium.

A

maximize: driver.maximize_window()
minimize: driver.minimize_window()
fullscreen: driver.fullscreen_window()

21
Q

When it is useful to take screenshots of the screen?

A
  • When the automated script detects that a failure occurred
  • When the pass/fail determination can only be made by viewing the screen image
  • When dealing with safety or mission-critical software which might require an audit
  • When doing configuration testing on different systems
22
Q

two scopes in which screenshots can be taken?

A
  • the entire browser page

- a single element in the browser page

23
Q

CODE: how to take a screenshot of a whole page and place it in a specific location?

A

driver.get_screenshot_as_file(“C:\path\to\screenshot.png”)

24
Q

CODE: how to take screenshot of an element and saving it to a specific location?

A

ele = driver.find_element_by_id(‘btnLogin’)

ele.screenshot(“C:\path\to\element_screenshot.png”)

25
Q

CODE: create an image as a base64 encoded string version of the snapshot of the window

A

img_b64 = driver.get_screenshot_as_base64()

26
Q

CODE: create a binary string representation of the snapshot of the window

A

png_str = driver.get_screenshot_as_png()

27
Q

CODE: create an image as a base64 encoded string version of the snapshot of a web element

A
ele = driver.find_element_by_id('loginForm')
img_b64 = ele.screenshot_as_base64
28
Q

CODE: create an image as a base64 encoded string version of the snapshot of a web element

A
ele = driver.find_element_by_id('loginForm')
png_str = ele.screenshot_as_png()