Selenium Flashcards Preview

Selenium > Selenium > Flashcards

Flashcards in Selenium Deck (65)
Loading flashcards...
1
Q

What is the difference between Relative XPATH and Absolute XPATH?

A
  • Absolute Xpath: It contains the complete path from the Root Element to the desire element.
  • Relative Xpath: You can simply start by referencing the element you want and go from there.
2
Q

What is a Stale Element?

A

an old element or no longer available element. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.

3
Q

How do you Press Keyboard Keys

A

We can pass Keys.ENTER or Keys.RETURN to the sendKeys method for that textbox.

textbox.sendKeys(Keys.RETURN);

4
Q

How do you switch windows

A
driver.switchTo().window(handle);
//Switch back to the parent window 
driver.switchTo().window(parentHandle);
5
Q

How do you handle JavaScript popups

A

Alert alert = driver.switchTo().alert();

alert.accept();

6
Q

What is the difference between getWindowHandle() and getWindowHandles()

A

“getWindowHandle()” method will provide a unique identifier(handle) of the current browser window which is being controlled by the WebDriver and return type is string

Whereas “getWindowHandles()” will provide a set(collection) of all existing window identifiers(handles) present at a given time and its return type is Set

7
Q

How do you execute JavaScript Commands

A

js = (JavascriptExecutor) driver;

js.executeScript(“window.location= https://letskodeit.teachable.com/pages/practice’”);

WebElement textBox = (WebElement).js.executeScript(“returndocument.getElementById(‘name’);”);

textBox.sendKeys(“test”);

8
Q

How do you find size of the Window with javaScript

A

long height = (Long)js.executeScript(“return window.innerHeight;”);

9
Q

What is the difference between Implicit Wait and Explicit Wait?

A

If elements are not immediately available, an implicit wait tells Web Driver to poll the DOM for a certain amount of time. The default setting is 0. Once set, the implicit wait is set for the duration of the Web Driver object

Implicit means we set a certain amount of time
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

Explicit waits is when we find a certain condition to occur before proceeding

10
Q

What are examples of Explicit Wait conditions?

A

alertisPresent
elementToBeClickable
presenceOfElementLocated
visbilityOfElementLocated

11
Q

If you use Implicit Wait of 10 seconds and explicit wait of 20 seconds, how much time Selenium WebDriver will wait before timing out?

A

The code will time out
It is unpredictable

It is not recommended to use both Explicit and Implicit waits in the same code

12
Q

How to get text on an Element ?

A

WebElement buttonElement = driver.findElement(By.id(“opentab”));
String elementText = buttonElement.getText();

13
Q

How to get Value of Element attribute?

A

driver.get(baseUrl);

WebElement element = driver.findElement(By.id(“name”));

String attributeValue = element.getAttribute(“type”);

System.out.println(“Value of attribue is: “ + attributeValue);

14
Q

What is profile?

A

It is the file where Firefox saves your personal information such as bookmarks, passwords, and user preferences
You sometimes need new profile for automation because
Of the need of some plugins in your web application or some SSL certificate for your application
Separate profile does not disturb your usual browser settings

15
Q

What is the difference between single slash / and double slash // ?

A

Single Slash means anywhere in xpath signifies to look for the element immediately inside the parent element
Single slash ( / ) start selection from the document node
It allows you to create ‘absolute’ path expressions

Double Slash signifies to look for any child or nested child element inside the parent element
Double slash ( // ) start selection matching anywhere in the document
It enables to create ‘relative’ path expressions

16
Q

What is Page Object Model

A

Page Object Model is a design pattern to create Object Repository for web UI elements. Under this model, for each web page in the application, there should be corresponding page class.

It helps make the code more readable, maintainable, and reusable.
POM creates a separate class file that would find web elements so if any changes happen, you only need to focus on this one class instead of 10 different scripts
17
Q

What is Object Repository?

A

object repository is a common storage location for all objects

The major advantage of using object repository is the segregation of objects from test cases.

object repository is independent of test cases

18
Q

What are TestNG Assert.assertTrue()

A

Assert true statement fails the test and stop the execution of the test if the actual output is false

19
Q

What are TestNG Assert.assertFalse()

A

Assert.assertFalse() works if you want your test to continue only if when some certain element is not present on the page. You will use Assert false, so it will fail the test in case of the element present on the page.

20
Q

What are Assert.assertEquals()

A

It will also stop the execution if the value is not equal and carry on the execution if the value is equal.

String expectedString = "Hello World";
SomeClassToTest obj = new SomeClassToTest();
String result = obj.addStrings("Hello", "World");
Assert.assertEquals(result, expectedString);
21
Q

What is the difference between HARD and SOFT Assertions ?

A

A Hard Assertion is type of assertion that throws an exception immediately when an assert statement fails and continues with the next test in the test suite.

Soft Assertions are the type of assertions that do not throw an exception when an assertion fails and would continue with the next step after assert statement. This is usually used when our test requires multiple assertions to be executed and the user want all of the assertions/codes to be executed before failing/skipping the tests.

22
Q

What is TimeoutException in WebDriver

A

This exception is thrown when a command performing an operation does not complete in the stipulated time

23
Q

What is NoSuchElementException in WebDriver

A

This exception is thrown when an element with given attributes is not found on the web page

24
Q

What is ElementNotVisibleException in WebDriver

A

This exception is thrown when the element is present in DOM (Document Object Model), but not visible on the web page

25
Q

What is StaleElementException in WebDriver

A

This exception is thrown when the element is either deleted or no longer attached to the DOM

26
Q

What is exception test in Selenium?

A

An exception test is an exception that you expect will be thrown inside a test class.

27
Q

What is Page Factory ?

A

Page Factory is used to initialize the elements of the Page Object or instantiate the Page Objects itself. Annotations for elements can also be created (and recommended) as the describing properties may not always be descriptive enough to differentiate one object from the other.

28
Q

What is the use of JavaScriptExecutor?

A

JavaScriptExecutor is an interface which provides a mechanism to execute Javascript through the Selenium WebDriver.

It provides “executescript” and “executeAsyncScript” methods, to run JavaScript in the context of the currently selected frame or window. An example of that is:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);

29
Q

How to scroll down a page using JavaScript in Selenium?

A

((JavascriptExecutor) driver).executeScript(“window.scrollBy(0,500)”);

30
Q

How to scroll down to a particular element?

A

((JavascriptExecutor) driver).executeScript(“arguments[0].scrollIntoView();”, element);

31
Q

What are the different types of frameworks?

A

Data Driven Framework
Keyword Driven Framework
Hybrid Framework

32
Q

What is Data Driven Framework

A

When the entire test data is generated from some external files like Excel, CSV, XML or some database table, then it is called Data Driven framework.

33
Q

What is Keyword Driven Framework

A

When only the instructions and operations are written in a different file like an Excel worksheet, it is called Keyword Driven framework.

34
Q

What is Hybrid Framework

A

A combination of both the Data Driven framework and the Keyword Driven framework is called Hybrid framework.

35
Q

How can you fetch an attribute from an element? How to retrieve typed text from a textbox?

A

We can fetch the attribute of an element by using the getAttribute() method. Sample code:

WebElement eLogin = driver.findElement(By.name(“Login”);
String LoginClassName = eLogin.getAttribute(“classname”);

36
Q

Can Selenium handle window pop-ups?

A

dismiss(): This method is called when the ‘Cancel’ button is clicked in the alert box.

accept(): This method is called when you click on the ‘OK’ button of the alert.

getText(): This method is called to capture the alert message.

sendKeys(String stringToSed): This is called when you want to send some data to alert box.

37
Q

What are Assert and Verify commands?

A

Assert: An assertion is used to compare the actual result of an application with the expected result.

Verify: There won’t be any halt in the test execution even though the verify condition is true or false.

38
Q

Can you navigate back and forth the webpage in Selenium?

A

driver. navigate.forward
driver. manage.back
driver. manage.navigate
driver. navigate.to(“url”)

39
Q

How to send ALT/SHIFT/CONTROL key in Selenium WebDriver?

A

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)

Purpose: Performs a modifier key press and does not release the modifier key. Subsequent interactions may assume it’s kept pressed.

Parameters: Modifier_key (keys.ALT or Keys.SHIFT or Keys.CONTROL)
Purpose: Performs a key release.
Hence with a combination of these two methods, we can capture the special function of a particular key.

40
Q

How to set the size of browser window using Selenium?

A

driver.manage().window().maximize(); – To maximize the window

you can use the setSize() method

System.out.println(driver.manage().window().getSize());
Dimension d = new Dimension(420,600);
driver.manage().window().setSize(d);
41
Q

How to switch to a new window (new tab) which opens up after you click on a link?

A

If you click on a link in a web page, then for changing the WebDriver’s focus/ reference to the new window we need to use the switchTo() command.

driver.switchTo().window();

In case you do not know the name of the window, then you can use the driver.getWindowHandle() command t

42
Q

Can we enter text without using sendKeys()?

A

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript(“document.getElementById(‘Login’).value=Test text without sendkeys”);

43
Q

Explain how you will login into any site if it is showing any authentication popup for username and password?

A

Since there will be popup for logging in, we need to use the explicit command and verify if the alert is actually present. Only if the alert is present, we need to pass the username and password credentials.

WebDriverWait wait = new WebDriverWait(driver, 10); 
Alert alert = wait.until(ExpectedConditions.alertIsPresent()); 
alert.authenticateUsing(new UserAndPassword(**username**, **password**));
44
Q

Explain how can you find broken links in a page using Selenium WebDriver? Interviewer can provide a situation where in there are 20 links in a web page, and we have to verify which of those 20 links are working and how many are not working (broken).

A

You need to send HTTP requests to all of the links on the web page and analyze the response.

Whenever you use driver.get() method to navigate to a URL, it will respond with a status of 200 – OK.

200 – OK denotes that the link is working and it has been obtained. If any other status is obtained, then it is an indication that the link is broken.

First, we have to use the anchor tags <a> to determine the different hyperlinks on the web page. For each </a><a> tag, we can use the attribute ‘href’ value to obtain the hyperlinks and then analyze the response received for each hyperlink when used in driver.get() method
</a>

45
Q

Which technique should you consider using throughout the script “if there is neither frame id nor frame name”?

A

If neither frame name nor frame id is available, then we can use frame by index.

Each frame will have an index number. The first frame would be at index “0”, the second at index “1” and the third at index “2”

driver.switchTo().frame(int arg0);

46
Q

How do you achieve synchronization in WebDriver?

A

It is a mechanism which involves more than one components to work parallel with each other.

Unconditional: In this we just specify timeout value only. We will make the tool to wait until certain amount of time and then proceed further.

Conditional: It specifies a condition along with timeout value, so that tool waits to check for the condition and then come out if nothing happens.

47
Q

How to handle keyboard and mouse actions using Selenium?

A

clickAndHold() -
Clicks (without releasing) the current mouse location.

dragAndDrop() -
Performs click-and-hold at the location of the source element, moves.

source, target() –

Moves to the location of the target element, then releases the mouse.

48
Q

What is Selenese?

A

Selenese is the set of selenium commands which are used to test your web application.

Actions: Used for performing operations
Assertions: Used as checkpoints
Accessors: Used for storing a value in a particular variable

49
Q

What is the difference between findElement() and findElements()

A
  • findElement() method returns a webElement object. It finds the first element within the current page
  • findElements() method returns a list of type . It find all the elements within the current page.webElement
50
Q
On the webpage, you have 5 text fields that all have the same ids and class 
names. How do you locate and act on the 3rd text field and input data into it?
A

findElements() to locate the list of the 5 text fields and then specify the 3rd element of the list (or the 2nd index) and then use the sendKeys() method to input data into the text field.

51
Q

I’m trying to input data to text fields on a webpage. The page has 4 text fields
that have the same id and classes, there is nothing different about them. The problem is that when I navigate to the page, there is an overlay that appears for 3 seconds and then goes away. Only when the overlay goes away can I access the text fields, how do I automate that?

A

Find the locator of the overlay (via id, css selector or xpath) and store it into a vari- able (storing the variable is optional but good practice). Then I implement a new Webdriver wait (because I’m waiting for the overlay to disappear not appear so I can’t use implicit wait, also see below usage of the Webdriver wait)

I  give it the wait a maximum of 6 seconds (or 10) and then I make a list of type 
webElement to capture the list of text fields and store into a variable. I implement a for loop (see below) to input text into all the text fields. 
- new WebDriverWait(driver,10).until(ExpectedConditions.invisibilityOfEle- 
mentLocated(locator)); 
- for(i=0; i
52
Q

My application has about 10 pages in it and I seem to be repeating so many actions on each page. I find myself just writing the same code for every page. The page has radio buttons and I need to click yes or no across all of them for a different test. Is there a way to optimize this code?

A
I would create a separate class (either utilities or helperMethods) and inside of it I 
would create a method to handle such repetitive behavior. Inside the class I would create a method (any method name) and give it a parameter of a webElement list. Inside that method I would have a for loop to navigate through each element and click either a yes or no for each radio button. 

In the Page classes now I would have 2 webElement Lists, one for the radio buttons
with yes values and one for the radio buttons with no values. In the base class (which all pages extend) I would create an object for the Utilities class.

Now whenever I have a page with radio buttons all I would have to do is call that method from the utilities class and pass in the specific webElementList.

class Utilities {

public void radioButtonHelper( ArrayList list) {

for (int i=o, i yesButtons = …;

private ArrayList noButtons = …;

public void fillOutPage() {

utilities.radioButtonHelper(yesButtons);

53
Q

I have hidden elements on the page, they are 5 radio buttons and then there is a text field that is displayed only after the 5 radio buttons are all selected. How how do I enter text to that text field in this scenario. Only the first radio button is visible, after that is selected the next one is displayed and so on until all 5 ra- dio buttons are selected and then the text field shows up on the webpage. (4-5 minutes)

A

I would first grab the common locator of all the radio buttons and then store them into a list of webElements. Afterwards I would create a for loop in which I would first check if the element is displayed and then click the element. Afterwards I would then have a if statement to check if the text field is displayed and if it’s dis- played I would fill in the data.

  • You can then further ask that you want to make sure that the radio buttons appear after a maximum of 3 seconds. In which the loop and the if statement had to be modified with waitUntilVisible methods giving a maximum wait time of 3 seconds.
54
Q

I have a few links on the page and when you click them they each open to a new tab. I want to validate that each link has the correct title. All the links are in order, the first link will always have the a certain title and so on. How do I validate that?

A

First I would create either an Array or ArrayList to store all the titles that I’ll need
to validate against. Then I would create a for loop that would handle all my links, inside the for loop I would have logic to handle switching to a new tab, validating the title, closing the tab and switching back to my original window.

class Demo { 
string[] titles = {.......}; ArrayList links = ...; 
public void validateLinks() { 
for(int i; i windows = new ArrayList(driver.getWindowHandles()); driver.switchTo().window(windows.get(1)); assertEquals(links[i].getTitle(), titles[i]); driver.close(); driver.switchTo().window(windows.get(0)); 
}
55
Q

What is Selenium and what is composed of?

Selenium is a suite of tools for automated web testing. It is composed of

A

Selenium IDE (Integrated Development Environment) : It is a tool for recording and playing back. It is a firefox plugin

WebDriver and RC: It provide the APIs for a variety of languages like Java, .NET, PHP, etc. With most of the browsers Webdriver and RC works.

Grid: With the help of Grid you can distribute tests on multiple machines so that test can be run parallel which helps in cutting down the time required for running in browser test suites

56
Q

How will you find an element using Selenium?

A
ID
Name
Tag
Attribute
CSS
Linktext
PartialLink Text
Xpath etc
57
Q

What is the difference between type keys and type commands ?

A

TypeKeys() will trigger JavaScript event in most of the cases whereas .type() won’t. Type key populates the value attribute using JavaScript whereas .typekeys() emulates like actual user typing

58
Q

What is JUnit Annotations and what are different types of annotations which are useful ?

A

special form of syntactic meta-data can be added to Java source code

Test
Before
After
Ignore
BeforeClass
AfterClass
RunWith
59
Q

What are the four parameter you have to pass in Selenium?

A

Host
Port Number
Browser
URL

60
Q

What is the difference between setSpeed() and sleep() methods?

A

Both will delay the speed of execution.

Thread.sleep () : It will stop the current (java) thread for the specified period of time. Its done only once

It takes a single argument in integer format
It waits only once at the command given at sleep
Ex: thread.sleep(2000)- It will wait for 2 seconds

SetSpeed () : For specific amount of time it will stop the execution for every selenium command.

Ex: selenium.setSpeed(“2000”)- It will wait for 2 seconds
Runs each command after setSpeed delay by the number of milliseconds mentioned in set Speed
This command is useful for demonstration purpose or if you are using a slow web application

61
Q

Which attribute you should consider throughout the script in frame for “if no frame Id as well as no frame name”?

A

You can use…..driver.findElements(By.xpath(“//iframe”))….

This will return list of frames.

You will need to switch to each and every frame and search for locator which we want.

Then break the loop

62
Q

Explain how you can switch between frames?

A

To switch between frames webdrivers [ driver.switchTo().frame() ] method takes one of the three possible arguments

A number: It selects the number by its (zero-based) index
A name or ID: Select a frame by its name or ID
Previously found WebElement: Using its previously located WebElement select a frame

63
Q

What is regular expressions? How you can use regular expressions in Selenium ?

A

A regular expression is a special text string used for describing a search pattern.

regexp: as a prefix to the value and patterns needs to be included for the expected values.

64
Q

Explain what is the main difference between web-driver and RC ?

A

The main difference between Selenium RC and Webdriver is that, selenium RC injects javascript function into browsers when the page is loaded. On the other hand, Selenium Webdriver drives the browser using browsers built in support

65
Q

How do you identify an object using selenium?

A

To identify an object using Selenium you can use

isElementPresent(String locator)

isElementPresent takes a locator as the argument and if found returns a Boolean