Selenium Flashcards
What are the Selenium suite components?
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.
What are the limitations of Selenium testing?
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.
What are the testing types supported by Selenium?
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.
What is Selenese? How is it classified?
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
Mention the types of Web locators.
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”);
What are the types of waits supported by WebDriver?
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);
Mention the types of navigation commands
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.
What is the major difference between driver.close() and driver.quit()?
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 to type text in an input box using Selenium?
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 to click on a hyperlink in Selenium?
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 to scroll down a page using JavaScript?
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 to assert the title of a webpage?
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 to mouse hover over a web element?
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 to retrieve CSS properties of an element?
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”);
What is POM (Page Object Model)?
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.