Selenium Flashcards

1
Q

What are the Selenium suite components?

A

Selenium IDE

It is a Firefox/Chrome plug-in that was developed to speed up the creation of automation scripts. It records the user actions on the web browser and exports them as a reusable script.

Selenium Remote Control (RC)

RC is a server that allows users to write application tests in various programming languages. The commands from the test script are accepted by this server and are sent to the browser as Selenium core JavaScript commands. The browser then behaves accordingly.

Selenium WebDriver

WebDriver is a programming interface that helps create and run test cases. It makes provision to act on web elements. Unlike RC, WebDriver does not require an additional server and interacts natively with the browser applications.

Selenium Grid

The grid was designed to distribute commands to different machines simultaneously. It allows the parallel execution of tests on different browsers and different operating systems. It is exceptionally flexible and is integrated with other suite components for simultaneous execution.

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

What are the limitations of Selenium testing?

A

1.Unavailability of reliable tech support: Since Selenium is an open-source tool, it does not have dedicated tech support to resolve the user queries.
2.Tests web applications only: Selenium needs to be integrated with third-party tools like Appium and TestNG to test desktop and mobile applications.
3.Limited support for image testing.
No built-in reporting and test management facility: Selenium has to be integrated with tools like TestNG, or JUnit among others to facilitate test reporting and management.

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

What are the testing types supported by Selenium?

A

Selenium supports Regression testing and Functional testing.

Regression testing - It is a full or partial selection of already executed test cases that are re-executed to ensure existing functionalities work fine.

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

What is Selenese? How is it classified?

A

Selenese is the set of Selenium commands which are used to test your web application. The tester can test the broken links, the existence of some object on the UI, Ajax functionality, alerts, window, list options, and a lot more using Selenese.

Action: Commands which interact directly with the application

Accessors: Allow the user to store certain values to a user-defined variable

Assertions: Verifies the current state of the application with an expected state

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

Mention the types of Web locators.

A

Locator is a command that tells Selenium IDE which GUI elements ( say Text Box, Buttons, Check Boxes, etc) it needs to operate on. Locators specify the area of action.

Locator by ID: It takes a string parameter which is a value of the ID attribute which returns the object to findElement() method.

driver.findElement(By.id(“user”));

Locator by the link: If your targeted element is a link text then you can use the by.linkText locator to locate that element.

driver.findElement(By.linkText(“Today’s deals”)).click();

Locator by Partial link: The target link can be located using a portion of text in a link text element.

driver.findElement(By.linkText(“Service”)).click();

Locator by Name: The first element with the name attribute value matching the location will be returned.

driver.findElement(By.name(“books”).click());

Locator by TagName: Locates all the elements with the matching tag name

driver.findElement(By.tagName(“button”).click());

Locator by classname: This finds elements based on the value of the CLASS attribute. If an element has many classes then this will match against each of them.

driver.findElement(By.className(“inputtext”));

Locator by XPath: It takes a parameter of String which is a XPATHEXPRESSION and it returns an object to findElement() method.

driver.findElement(By.xpath(“//span[contains(text(),’an account’)]”)).getText();

Locator by CSS Selector: Locates elements based on the driver’s underlying CSS selector engine.

driver.findElement(By.cssSelector(“input#email”)).sendKeys(“myemail@email.com”);

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

What are the types of waits supported by WebDriver?

A

Implicit wait - Implicit wait commands Selenium to wait for a certain amount of time before throwing a “No such element” exception.

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);

Explicit wait - Explicit wait is used to tell the Web Driver to wait for certain conditions before throwing an “ElementNotVisibleException” exception.

WebDriverWait wait = new WebDriverWait(WebDriver Reference, TimeOut);

Fluent wait - It is used to tell the web driver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an “ElementNotVisibleException” exception.

Wait wait = new FluentWait(WebDriver reference).withTimeout(timeout, SECONDS).pollingEvery(timeout, SECONDS).ignoring(Exception.class);

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

Mention the types of navigation commands

A

driver.navigate().to(“https://www.ebay.in/”); - Navigates to the provided URL

driver.navigate().refresh(); - This method refreshes the current page

driver.navigate().forward(); - This method does the same operation as clicking on the Forward Button of any browser. It neither accepts nor returns anything.

driver.navigate().back(); - This method does the same operation as clicking on the Back Button of any browser. It neither accepts nor returns anything.

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

What is the major difference between driver.close() and driver.quit()?

A

driver.close()

This command closes the browser’s current window. If multiple windows are open, the current window of focus will be closed.

driver.quit()

When quit() is called on the driver instance and there are one or more browser windows open, it closes all the open browser windows

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

How to type text in an input box using Selenium?

A

sendKeys() is the method used to type text in input boxes

Consider the following example -

WebElement email = driver.findElement(By.id(“email”)); - Finds the “email” text using the ID locator

email.sendKeys(“abcd.efgh@gmail.com”); - Enters text into the URL field

WebElement password = driver.findElement(By.id(“Password”)); - Finds the “password” text using the ID locator

password.sendKeys(“abcdefgh123”); - Enters text into the password field

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

How to click on a hyperlink in Selenium?

A

driver.findElement(By.linkText(“Today’s deals”)).click();

The command finds the element using link text and then clicks on that element, where after the user would be redirected to the corresponding page.

driver.findElement(By.partialLinkText(“Service”)).click();

The above command finds the element based on the substring of the link provided in the parenthesis and thus partialLinkText() finds the web element.

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

How to scroll down a page using JavaScript?

A

scrollBy() method is used to scroll down the webpage

General syntax:

executeScript(“window.scrollBy(x-pixels,y-pixels)”);

First, create a JavaScript object

JavascriptExecutor js = (JavascriptExecutor) driver;

Launch the desired application

driver.get(“https://www.amazon.com”);

Scroll down to the desired location

js.executeScript(“window.scrollBy(0,1000)”);

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

How to assert the title of a webpage?

A

Get the title of the webpage and store in a variable

String actualTitle = driver.getTitle();

Type in the expected title

String expectedTitle = “abcdefgh”;

Verify if both of them are equal

if(actualTitle.equalsIgnoreCase(expectedTitle))

System.out.println(“Title Matched”);

else

System.out.println(“Title didn’t match”);

Alternatively,

Assert.assertEquals(actualTitle, expectedTitle);

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

How to mouse hover over a web element?

A

Actions class utility is used to hover over a web element in Selenium WebDriver

Instantiate Actions class.

Actions action = new Actions(driver);

In this scenario, we hover over search box of a website

actions.moveToElement(driver.findElement(By.id(“id of the searchbox”))).perform();

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

How to retrieve CSS properties of an element?

A

getCssValue() method is used to retrieve CSS properties of any web element

General Syntax:

driver.findElement(By.id(“id“)).getCssValue(“name of css attribute”);

Example:

driver.findElement(By.id(“email“)).getCssValue(“font-size”);

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

What is POM (Page Object Model)?

A

Every webpage of the application has a corresponding page class that is responsible for locating the web elements and performing actions on them. Page Object Model is a design pattern that helps create object repositories for the web elements. POM improves code reusability and readability. Multiple test cases can be run on the object repository.

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

Can Captcha be automated?

A

No, Selenium cannot automate Captcha. Well, the whole concept of Captcha is to ensure that bots and automated programs don’t access sensitive information - which is why, Selenium cannot automate it. The automation test engineer has to manually type the captcha while other fields can be filled automatically.

17
Q

How does Selenium handle Windows-based pop-ups?

A

Selenium can handle Windows based pop up. There may be scenarios where a web page opens more than one window after performing some actions on it. The child windows that get opened may be a pop up containing some information or advertisement.

Selenium uses the getWindowHandles () and getWindowHandle () methods to work with child windows. The getWindowHandles () method contains all the window handle ids of the opened windows. The window id handles are held in the form of Set data structure [containing data type as String].

The getWindowHandle () method is used to store the window handle id of the present active window. As we know, getWindowHandles () method is used to store all the opened window handle ids. To iterate through all the handles, iterator () and next () methods are used.

Since getWindowHandles () stores the window ids in the form of Set data structure, we have to import java.util.Set in our code. Also, for using the iterator () method, we have to import java.util.Iterator and import java.util.List.

Finally to switch to a particular window, switchTo.().window() method is used. The handle id of the window where we want to switch is passed as an argument to that method.

The steps to be followed to implement the above concept −

After the application is launched, let us first store all the window handle ids in a Set data structure with the help of getWindowHandles () method.

We shall iterate through all the window handle ids with the help of iterator () and next () methods.

Let us then grab the current window handle id with the help of getWindowHandle () method.

Then switch to that window with switchTo.().window() method.

Example
Code Implementation.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
public class HandleWindows {
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “C:\Users\ghs6kor\Desktop\Java\chromedriver.exe”);
WebDriver driver = new ChromeDriver();
String url = “https://secure.indeed.com/account/login”;
driver.get(url);
//implicit wait
driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
// store all window handles
Set a = driver.getWindowHandles();
// iterate through handles
Iterator it = a.iterator();
String chlwnd = it.next();
String pwnd = it.next();
// switch to child window
driver.switchTo().window(chlwnd);
System.out.println(“Page title “+ driver.getTitle());
// switch to parent window
driver.switchTo().window(pwnd);
System.out.println(“Page title “+ driver.getTitle());
driver.quit();

18
Q

how to handle multiple window in webdriver?

A

String s=dr.getWindowHandle();

Set s1=dr.getWindowHandles();

for(String c:s1)

{

if(!c.equals(s))

{

dr.switchTo().windows(s);

}

}

19
Q

How to take screenshots in WebDriver?

A

TakeScreenshot interface can be used to take screenshots in WebDriver.

getScreenshotAs() method can be used to save the screenshot

File scrFile = ((TakeScreenshot)driver).getScreenshotAs(outputType.FILE);

20
Q

Is there a way to type in a textbox without using sendKeys()?

A

Yes! Text can be entered into a textbox using JavaScriptExecutor

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript(“document.getElementById(‘email’).value=“abc.efg@xyz.com”);

21
Q

How to select a value from a dropdown in Selenium WebDriver?

A

Select class in WebDriver is used for selecting and deselecting options in a dropdown.

The objects of Select type can be initialized by passing the dropdown webElement as a parameter to its constructor.

WebElement testDrop = driver.findElement(By.id(“testingDropdown”));

Select dropdown = new Select(testDrop);

WebDriver offers three ways to select from a dropdown:

selectByIndex: Selection based on index starting from 0

dropdown.selectByIndex(5);

selectByValue: Selection based on value

dropdown.selectByValue(“Books”);

selectByVisibleText: Selection of option that displays text matching the given argument

dropdown.selectByVisibleText(“The Alchemist”);

//Bittech Ans:

Select s=new Select(dr.findElement(By.xpath(“xpath”)));

s.selectByVisibleText();

or

s.selectByIndex();

//for size

s.getOption().size();

22
Q

What does the switchTo() command do?

A

`switchTo() command is used to switch between windows, frames or pop-ups within the application. Every window instantiated by the WebDriver is given a unique alphanumeric value called “Window Handle”.

Get the window handle of the window you wish to switch to

String handle= driver.getWindowHandle();

Switch to the desired window

driver.switchTo().window(handle);

Alternatively

for(String handle= driver.getWindowHandles())

{ driver.switchTo().window(handle); }

23
Q

How to upload a file in Selenium WebDriver?

A

You can achieve this by using sendkeys() or Robot class method. Locate the text box and set the file path using sendkeys() and click on submit button

Locate the browse button

WebElement browse =driver.findElement(By.id(“uploadfile”));

Pass the path of the file to be uploaded using sendKeys method

browse.sendKeys("D:\\SeleniumInterview\\UploadFile.txt");
24
Q

How to set browser window size in Selenium?

A

The window size can be maximized, set or resized

To maximize the window

driver.manage().window().maximize();

To set the window size

Dimension d = new Dimension(400,600);

driver.manage().window().setSize(d);

Alternatively,

The window size can be reset using JavaScriptExecutor

((JavascriptExecutor)driver).executeScript(“window.resizeTo(1024, 768)”);

25
Q

How to login to any site if it is showing an Authentication Pop-Up for Username and Password?

A

To handle authentication pop-ups, verify its appearance and then handle them using an explicit wait command.

Use the explicit wait command

WebDriverWait wait = new WebDriverWait(driver, 10);

Alert class is used to verify the alert

Alert alert = wait.until(ExpectedConditions.alertIsPresent());

Once verified, provide the credentials

alert.authenticateUsing(new UserAndPassword(, ));

Bittech:

pass the username and password with url.

http://username:password@urlhttp://creyate:jamesbond007@alpha.creyate.com

Syntax- http://username:password@url

ex- http://creyate:jamesbond007@alpha.creyate.com

26
Q
  1. What is the difference between single and double slash in Xpath?
A

Single slash is used to create Xpath with an absolute path i.e. the XPath would be created to start selection from the start node.

/html/body/div[2]/div[1]/div[1]/a

Double slash is used to create Xpath with relative path i.e. the XPath would be created to start selection from anywhere within the document

//div[class=”qa-logo”]/a

27
Q

How do you find broken links in Selenium WebDriver?

or How would you get the status code of all the links in a webpage?

A

When we 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.

Some of the HTTP status codes are :

200 – valid Link
404 – Link Not Found
400 – Bad Request
401 – Unauthorized
500 – Internal error
As a starter, obtain the links from the web application, and then individually get their status.

Navigate to the interested webpage for e.g. www.amazon.com

Collect all the links from the webpage. All the links are associated with the Tag ‘a‘

List links = driver.findElements(By.tagName(“a”));

Create a list of type WebElement to store all the Link elements in it.

for(int i=0; i

28
Q

What do you mean by the assertion in Selenium?

A

An assertion is a method of testing whether a particular condition is true or false. In Selenium, assertions are used to verify the state of elements on a page or the results of an action. Assertions can be used to check for the presence or absence of an element, the value of an element, or the text of an element. Assertions can also be used to check that an element is visible or hidden.

Assertions are an important part of testing with Selenium, as they enable you to verify that the state of your application meets your expectations. Without assertions, it would be difficult to know whether or not your tests are actually passing or failing.

29
Q

Explain the difference between assert and verify commands.

A

The assert command is used to check if the given condition is true or not. If the condition is true, then the execution of the program will continue. If the condition is false, then the execution of the program will stop.

The verify command is used to check if the given condition is true or not. If the condition is true, then the execution of the program will continue. If the condition is false, then the execution of the program will not stop, but an error message will be displayed.

30
Q

Explain XPath Absolute and XPath attributes.

A

XPath has two main types of expressions: absolute and relative. Absolute expressions always start with a forward slash (/), which indicates the root element of the document. Relative expressions do not start with a forward slash, and are relative to the current context.

Attributes are another important part of XPath. Attributes are added to elements and can contain valuable information about that element. In order to access an attribute, you must use the at sign (@) followed by the attribute name.

31
Q

What is the difference between “/” and “//” in XPath?

A

The difference between “/” and “//” in XPath is that “/” is used to select an element based on its absolute location, while “//” is used to select an element based on its relative location.

For example, if you want to select the first <p> element on a page, you would use “/p”. If you want to select all </p><p> elements on a page, regardless of their location, you would use “//p”.</p>

32
Q

What are the different types of annotations which are used in Selenium?

A

Different types of annotations that are used in Selenium include:

@Test - This annotation is used to mark a method as a test method
@BeforeMethod - This annotation is used to execute a method before each test method
@AfterMethod - This annotation is used to execute a method after each test method
@BeforeClass - This annotation is used to execute a method before the first test method