Selenium Flashcards
What is the difference between Relative XPATH and Absolute XPATH?
- 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.
What is a Stale Element?
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.
How do you Press Keyboard Keys
We can pass Keys.ENTER or Keys.RETURN to the sendKeys method for that textbox.
textbox.sendKeys(Keys.RETURN);
How do you switch windows
driver.switchTo().window(handle); //Switch back to the parent window driver.switchTo().window(parentHandle);
How do you handle JavaScript popups
Alert alert = driver.switchTo().alert();
alert.accept();
What is the difference between getWindowHandle() and getWindowHandles()
“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
How do you execute JavaScript Commands
js = (JavascriptExecutor) driver;
js.executeScript(“window.location= https://letskodeit.teachable.com/pages/practice’”);
WebElement textBox = (WebElement).js.executeScript(“returndocument.getElementById(‘name’);”);
textBox.sendKeys(“test”);
How do you find size of the Window with javaScript
long height = (Long)js.executeScript(“return window.innerHeight;”);
What is the difference between Implicit Wait and Explicit Wait?
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
What are examples of Explicit Wait conditions?
alertisPresent
elementToBeClickable
presenceOfElementLocated
visbilityOfElementLocated
If you use Implicit Wait of 10 seconds and explicit wait of 20 seconds, how much time Selenium WebDriver will wait before timing out?
The code will time out
It is unpredictable
It is not recommended to use both Explicit and Implicit waits in the same code
How to get text on an Element ?
WebElement buttonElement = driver.findElement(By.id(“opentab”));
String elementText = buttonElement.getText();
How to get Value of Element attribute?
driver.get(baseUrl);
WebElement element = driver.findElement(By.id(“name”));
String attributeValue = element.getAttribute(“type”);
System.out.println(“Value of attribue is: “ + attributeValue);
What is profile?
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
What is the difference between single slash / and double slash // ?
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
What is Page Object Model
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
What is Object Repository?
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
What are TestNG Assert.assertTrue()
Assert true statement fails the test and stop the execution of the test if the actual output is false
What are TestNG Assert.assertFalse()
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.
What are Assert.assertEquals()
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);
What is the difference between HARD and SOFT Assertions ?
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.
What is TimeoutException in WebDriver
This exception is thrown when a command performing an operation does not complete in the stipulated time
What is NoSuchElementException in WebDriver
This exception is thrown when an element with given attributes is not found on the web page
What is ElementNotVisibleException in WebDriver
This exception is thrown when the element is present in DOM (Document Object Model), but not visible on the web page
What is StaleElementException in WebDriver
This exception is thrown when the element is either deleted or no longer attached to the DOM
What is exception test in Selenium?
An exception test is an exception that you expect will be thrown inside a test class.