Selenium Flashcards
How many changes do you need to make to manage your Selenium scripts? How do you maintain your test scripts and how frequently do you have to modify them?
It all depends on the types of changes. As an example, in our Page Object Model (POM) design, if a web element changes, we only update that specific element in its page class but, if there’s a change in existing functionality, we update the relevant functions. When we find new changes leading to test failures, then we update our test scripts.
What have you done to improve the performance of the selenium framework?
We created reusable elements, wrote efficient functions with clear steps, followed coding standards, wrote locators to handle dynamic elements, and provided data in external files such as Excel, Text files, and property files.
What is Selenium? Why do you prefer the Selenium Automation Tool? What are the common test automation tools? What are the testing types that can be supported by Selenium? Challenges, issues and limitations of Selenium?
Selenium is a set of open-source tools for automating web browsers, commonly used for functional and regression test automation across different operating systems (Windows, Linux, Macintosh). It supports various browsers like Mozilla Firefox, Internet Explorer, Google Chrome, Safari, and Opera. Selenium also provides compatibility with multiple programming languages, including Java, C#, Python, Perl, Ruby, PHP, and JavaScript. While Selenium and QTP are widely used, there are scenarios where Selenium WebDriver may not be suitable, including challenges in automating Captcha, reading barcodes, handling Windows-based pop-ups, image validation and comparison, and PDF validation and comparison
What is Selenium IDE? What is Selenium RC? What is Selenium Grid? When do you use Selenium Grid? What are the advantages of Selenium Grid? Which version of Selenium Webdriver are you using?
Selenium IDE is a Firefox plugin, Selenium RC (Selenium 1) is Selenium Remote Control, Selenium WebDriver (Selenium 2) is a browser automation framework using browser-specific drivers, and Selenium Grid is a tool for parallel test execution on multiple platforms. The advantages of Selenium Grid include the ability to run test cases in parallel, conduct multi-browser testing, and execute test cases on multiple platforms concurrently. Currently using Selenium 3.0 due to compatibility, a Maven project aids in easily updating dependencies through the pom.xml file.
What are the types of WebDriver APIs available in Selenium? Which WebDriver implementation claims to be the fastest? What is the super interface of WebDriver? What is a headless browser?
The webdriver APIs in Selenium include Gecko Driver, InternetExplorer Driver, Chrome Driver, Safari Driver, HTMLUnit Driver. HTMLUnitDriver fastest because it is headless browser which have less User Interface, the Super interface of WebDriver is SearchContext Interface.
What are the Locators available in Selenium? Which locator do you prefer?
There are eight locators in Selenium for identifying WebElements on a webpage: ID, ClassName, Name, TagName, LinkText, PartialLinkText, XPath, and CSS Selector. The choice of locator preference depends on the specific requirements and characteristics of the project.
What is an XPath? What is the difference between Absolute Path and Relative Path? What is the difference between “/” and “//”? In which situations are you going to use Xpath?
XPath is a tool in Selenium for locating elements, offering two types: Absolute XPath, using a single slash “/”, starting from the document node, and Relative XPath, using double slashes “//,” beginning selection from anywhere within the document. For example, an Absolute XPath could be “/html/body/td/tr/div[1]/div[2],” while a Relative XPath might be “//input[@id=’username’].”
How to launch a browser using Selenium WebDriver? Explain the line of code
// set the property for chrome driver, specify its location via the webdriver.chrome.driver -> System.setProperty(“we driv-er.chrome.driver”, driverPath + “chromedriver.exe”); // instantiate an instance of ChromeDriver, which will be driving our browser: -> public static WebDriver driver = new ChromeDriver(); // navigate to the specific url -> driver.get(“http://google.com”); To launch a Chrome browser with Selenium WebDriver, this code sets the ChromeDriver’s location, creates a ChromeDriver instance, and navigates to “http://google.com”.
Write a complex xpath or css expression? Which one is better? What is the difference?
XPath:”//label[@for=’personal_txtLicExpDate’]/following-sibling::img” CSS: “input[name^=’pass’]” Both CSS and XPath are valuable in Selenium automation. CSS selectors are often better for Internet Explorer, while XPath is generally preferred for other browsers like Firefox, Chrome, and Safari. Differences: CSS is generally more user-friendly and readable than XPath. CSS operates only in a forward direction, while XPath allows searching elements backward or forward. XPath can work with text, a capability not present in CSS. In terms of speed, CSS and XPath are generally comparable, but CSS is often noticeably faster than XPath in Internet Explorer, based on practical experiences.
What is the alternative to driver.get() method to open a URL using Selenium WebDriver? What is the difference?
To open a URL in Selenium WebDriver, we can use either driver.get(url) or driver.navigate().to(url). The difference is driver.get(url) directly opens the URL and waits for the page to load, but driver.navigate().to(url) provides more navigation control without waiting for the page to load immediately.
What is the difference between driver.close() and driver.quit() methods?
close() - It is used to close the browser or page which is currently having the focus. quit() - It is used to shut down the web driver instance or destroy the webdriver instance (Close all the windows).
How to get the text of a web element? How to get an attribute value using Selenium WebDriver?
To get the text of a web element using Selenium WebDriver, we can use the getText() method, and to retrieve the value of an attribute, or we can use the getAttribute(attributeName) method. String buttonText =driver.findElement(By.cssSelector(“div.success”)).getText();StringinnerText=driver.findElement(By.cssSelector(“div.success”)).getAttribute(“type”);
What is the difference between driver.findElement() and driver.findElements() commands? What is the return type of findElements?
findElement() will return only a single WebElement and if that element is not located or we use some wrong selector then it will throw NoSuchElementexception. findElements() will return List of WebElements – It returns an empty list, when an element is not found on the current page as per the given element locator mechanism. It doesn’t throws NoSuchElementException
How do you work with radio buttons which do not have an id attribute?
We can use click(), if “id” is not present we can use xpath (i.e you can use relative or absolute), css or other locator types. Also when there is a group of Radio Buttons/Check Boxes on the page then, their names may be the same, but values are different. In that case, we can use the Webdriver findElements command to get the list of web elements and then loop through them.
How do you handle an element in different windows? GetWindowHandle vs GetWindowHandles and the return types. Write a method to switch to the window based on title?
We can handle multiple windows in selenium webdriver using Switch To methods which will allow us to switch control from one window to another window. When we open the browser window and then 3 tabs pop open, selenium is focused on the first window. getWindowHandle() will get the handle of the page the webDriver is currently controlling. Every time Selenium opens a browser, it’s going to give an index ID for the page called handle. This handle is a unique identifier for the web page. This is different every time you open a page even if it is the same URL. String handle = driver.getWindowHandle(); getWindowHan-dles() method or command returns all handles from all opened browsers by Selenium WebDriver during execution Set<String> handleSet = driver.getWindowHandles(); //Returns a set of window handles You can use SwitchTo().Window("handle") to switch to the window you desire. You can use Switch-To().Window("mywindowID"), if you know the window ID. SwitchTo().Window("") will always go back to the base/main window. Method to switch to a window based on title public switchToWindow(String titleName) { driv-er.switchTo().window(titleName); //titleName = windowName }</String>
How to select a value in a dropdown? How to check the multiple selected value in a dropdown?
We can use the Select class to interact with dropdowns in Selenium WebDriver, providing methods such as selectByVisibleText(), selectByIndex(), and selectByValue() for different selection options. Additionally, the isMultiple() method helps verify if the dropdown allows multiple selections, allowing conditional handling based on its behavior.
How would you handle elements that are in a different frame? How to identify the frame which does not have Id as well as name?
Using switch commands we can switch to different frame to handle elements: We can switch to the frame 3 different ways: 1. Switch to Frame by Name or ID driver.switchTo().frame(“iframe1”) 2. Switch to Frame by WebElement WebElement iframeElement = driver.findElement(By.id(“IF1”)); driv-er.switchTo().frame(iframeElement); 3. Switch to Frames by Index driver.switchTo().frame(0)
How can we handle web based pop-up? How can you handle JavaScript Alerts? Can we inspect an alert? How many ways can you handle alerts? How do you get text from an alert? How do you send a text to alert? How can we handle Windows-based pop-ups?
We can handle web based pop-ups using Alert Interface. There are four methods that we would be using along with the Alert interface. Alert alert = driver.switchTo().alert(); or driv-er.switchTo().alert() 1) void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop-up window appears. alert.dismiss(); or driver.switchTo().alert().dismiss(); 2) void accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears. alert.accept(); or driver.switchTo().alert().accept(); 3) String getText() – The getText() method returns the text displayed on the alert box. alert.getText(); ordriver.switchTo().alert().getText(); 4) void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box. alert.sendkeys(“text”); or driver.switchTo().alert().sendkeys(“text”); Selenium WebDriver cannot handle window based pop ups, for that we can use Robot Class or third-party tools like AutoIT.
What are the types of waits available in Selenium WebDriver? What is Implicit/ Explicit/ Fluent Wait In Selenium WebDriver? What is WebDriverWait In Selenium WebDriver?
Implicit Wait The implicit wait will tell the web driver to wait for a certain amount of time before it throws a “No Such Element Exception”. The default setting is 0. Once we set the time, the webdriver will wait for that time before throwing an “TimeOutEx-ception” exception. driv-er.manage().timeouts().implicitlyWait(TimeOut, TimeU-nit.SECONDS);
Explicit Wait The explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) or the maximum time exceeded before throwing an “ElementNotVisibleEx-ception” exception. The explicit wait is an intelligent kind of wait, but it can be applied only for specified elements. WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“ckdmc”));
Fluent Wait The fluent wait is used to tell the webdriver to wait for a condition, as well as the frequency with which we want to check the condition before throwing an “ElementNotVisibleExcep-tion” exception. Frequency: Setting up a repeat cycle with the time frame to verify/check the condition at the regular interval of time. Consider a scenario where an element is loaded at different intervals of time. The element might load within 10 seconds, 20 seconds or even more than that if we declare an explicit wait of 20 seconds. It will wait till the specified time before throwing an exception. In such scenarios, the fluentwait is the ideal wait to use as this will try to find the element at different frequencies until it finds it or the final timer runs out.
Wait wait = new FluentWait(WebDriver reference)
wait.withTimeout(Duration.ofSeconds(30));
wait.pollingEvery(Duration.ofSeconds(1));
wait.ignoring(NoSuchElementException.class);
How to pause a test execution for 5 seconds at a specific point?
By using Thread.sleep(5000);
How To Perform Right Click in Selenium WebDriver? Double Click? Hover over on a web element? Drag And Drop? What operations can you do using Actions class?
Using action class we can perform all of these operations Actions action = new Actions(driver);
WebElement element=driver.findElement(By.linkText(“TEST”));
//Double click action.doubleClick(element).perform();
//Mouse over action.moveToElement(element).perform();
//Right Click action.contextClick(element).perform();
//Drag and Drop WebElement element = driv-er.findElement(By.name(“source”));
WebElement target = driver.findElement(By.name(“target”));
action.dragAndDrop(element, target).perform();
We can perform following operation using action class:
● click (): Simply click on element
● doubleClick (): Double clicks on Element
● contextClick() : Perform a context-click (right click) on an element
● clickAndHold(): Clicks at the present mouse location (without releasing)
● dragAndDrop(source, target): Invokes click-and-hold at the source location and moves to the location of the target element before releasing the mouse. source – element to grab, target – element to release
How to handle hidden elements in Selenium WebDriver? Is there a way to click on a hidden LINK in WebDriver?
First, store that element in an object, let’s say element, and then write the following code to click on that hidden element WebElement element=”property of element”; JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript(“arguments[0].click();”, element);
How To Scroll Web Page Down Or Up Using Selenium WebDriver?
We use JavascriptExecutor // Scroll Up on application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(“window.scrollBy(0,250)”); // Scroll Down on application using Selenium JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(“window.scrollBy(0, -400)”); //to scroll an application to specified elements. JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript(“arguments[0].scrollIntoView(true);”, element);
How to input text in the text box using Selenium WebDriver? How to input text in the text box without calling the sendKeys()?
we can input text using sendkeys.WebElement email = driver.findElement(By.id(“email”)); email.sendKeys(“your@email.here”); as well as JavascriptExecu-tor je = (JavascriptExecutor)driver; je.executeScript(“document.getElementById(‘email’).value=’your@email.com’;”);