Selenium Flashcards
What is Selenium WebDriver?
Was the first cross-platform testing framework that could configure and control the browsers on the OS level. It served as a programming interface to create and run test cases.
WebDriver performs actions on web elements. It supports various programming languages like Java, C#, PHP, Python, among others. It can also be integrated with frameworks like TestNG and JUnit for test management
What is Selenium Grid?
A grid with the primary objective of minimizing the test execution time. This was facilitated by distributing the test commands to different machines simultaneously. Selenium Grid allows the parallel execution of tests on different browsers and different operating systems. Grid is exceptionally flexible and integrates with other suite components for simultaneous execution.
========
The Grid consists of a hub connected to several nodes. It receives the test to be executed along with information about the operating system and browser to be run on and picks a node that conforms to the requirements (browser and platform), passing the test to that node. The node now runs the browser and executes the selenium commands within it.
**Advantages of Selenium Testing **
- Selenium has proven to be accurate with results thus making it extremely reliable
- Since selenium is open-source, anybody willing to learn testing can begin at no cost
- Selenium supports a broad spectrum of programming languages like Python, PHP, Perl, and Ruby
- Selenium supports various browsers like Chrome, Firefox, and Opera, among others
- Selenium is easy to implement and doesn’t require the engineer to have in-depth knowledge of the tool
- Selenium has plenty of re-usability and add-ons
Limitations of Selenium Testing
- Since Selenium is open-source, it doesn’t have a developer community and hence doesn’t have a reliable tech support
- Selenium cannot test mobile or desktop applications
- Selenium offers limited support for image testing
- Selenium has limited support for test management. Selenium is often integrated with tools like JUnit and TestNG for this purpose
- You may need knowledge of programming languages to use Selenium
What is XPath?
XPath is a Selenium technique that is used to navigate through the HTML structure of a webpage. It is a syntax or language that makes finding elements on a webpage possible using XML path expression.
In Selenium automation, there may be times when elements cannot be found with general locators like ID, name, class, etc. And this is when XPath is used to locate those elements on the webpage. XPath in Selenium may be used on both XML and HTML documents.
Syntax of XPath
Syntax of XPath
XPath in Selenium provides many essential XPath functions and axes to write effective XPaths of the web elements and define unique locators.
XPath = //tagname[@Attribute = ‘Value’]
// : to select the current node.
tag name: tag name of a specific node.
@: to select the attribute.
attribute: it is the attribute name of the node.
value: it is the value of the node.
Types of XPath Locators
- ID - Find the element using the ID of the element
- Classname - Find the element using the class name of the element
- Link Text - Find the element using the text of the link
- Name - Find the element using the name of the element
- XPath - Required to find the dynamic element and to traverse between different elements of the web page
- CSS path - Locates elements that have no class. name, or ID
Types of XPath
Absolute XPath refers to the direct way of finding an element. The major drawback of Absolute XPath is that if there are any changes in the element’s path, then the XPath will fail.
The XPath begins with a single forward-slash (/), which states that the element can be selected from the root node.
Absolute XPath:/html/body/div[1]/div/div[2]/header/div/div[2]/a/img
==========
Relative XPath, the path begins from the middle of the HTML DOM structure. Here, the structure starts with a double forward-slash (//) that states that the element can be searched anywhere on the webpage.
Relative XPath enables you to write from the middle of the HTML DOM structure without any need to write a long XPath.
Relative XPath://*[@id=”block-perfecto-main-menu”]/ul/li[6]/a
XPath Contains() function
The XPath Contains() is a function used to create an XPath expression. It is used if part of the value of any attribute changes dynamically, like login information, etc. The function can navigate to the web element with the partial text present.
//tag_name[contains(@attribute,’value_of_attribute’)]
The Contains() method accepts two parameters:
- The attribute of the tag must validate to locate the web element.
- The value of an attribute is a partial value that the attribute must contain.
XPath Text() function
The XPath Text() is a function used to locate the element on a web page using the web element’s text. The function proves its worth if the element contains a text, like a label, etc.
//tag_name[text()= ’Text of the element’]
The text() method here returns the text of the web element when identified by the tag_name, and compared with the value provided on the right side.
XPath Starts-with() function
The XPath Starts-with() function is used to find the element in which the attribute value starts with some specific character or a sequence of characters. The function plays a major role while working with the dynamic web pages.
/tag_name[starts-with(@attribute,’Part_of_Attribute_value’)]
The Starts-with() accepts two parameters:
The attribute of the tag must validate to locate the web element.
The attribute value is the partial value of the attribute with which the attribute is expected to start.
What Are XPath Axes?
All the XML DOM elements are in a hierarchical structure and can be either located using Absolute paths or Relative paths. For this, XPath provides specific attributes called “XPath Axis.”
Here, an axis shows a relationship to the current node and helps locate the relative nodes concerning the tree’s current node. So, the XPath Axis uses the relation between several nodes to find those nodes in the DOM structure.
Let’s have a look at some Axis that helps in locating the elements on the webpage:
ancestor - To locate the ancestors of the current node, including the parent node and the root node
ancestor-or-self - To locate the current node along with its ancestors
attribute - To specify the attributes of the current node
child - To locate the child/children of the current node
descendant - To locate the children of the current node up the leaf node
descendant- or-self - To locate the current node with its descendants
following - To locate all the nodes that come after the current node
parent - To locate the parent of the current node
self - To locate the current (present) node
What is the same-origin policy and how is it handled?
Same Origin policy is a feature adopted for security purposes. According to this policy, a web browser allows scripts from one webpage to access the contents of another webpage provided both the pages have the same origin. The origin refers to a combination of the URL scheme, hostname, and port number.
The same Origin Policy prevents a malicious script on one page to access sensitive data on another webpage.
What is Selenese? How is it classified?
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
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);