Selenium is an open-source, automated testing tool that is used to test web applications across various browsers. It can test only web applications and not mobile and desktop applications.
Selenium is not just a single tool or utility, but rather a package of several testing tools and that’s why it’s called Selenium Suite.
Selenese which is called the Selenium command is the set of the Selenium commands that run your test cases. For example, open (URL) is a Selenium command that opens the specified URL in the specific browser. A test script is the sequence of all these commands put together.
Selenese commands are used to test the web applications. The tester can test the broken links, the existence of some objects on the UI, Ajax functionality, alerts, windows, list options, and a lot more using Selenese.
Selenium WebDriver supports several popular web browsers, including Google Chrome, Mozilla Firefox, Microsoft Edge, Safari, and Opera.
This command will instruct the Selenium WebDriver to open the specified URL in the web browser.
An alternative option to the driver.get() method to open a URL in Selenium Web Driver is to use the driver.navigate().to() method.
Locators in Selenium are methods used to identify and locate web elements on a web page. They are essential to automation testing as they help automate discovery and interaction with web elements.
Xpath or XML path is a query language that is used for selecting nodes from XML documents. Also, it is one of the locators supported by Selenium Webdriver.
X-Path is used to find the WebElement in web pages. It is also useful in identifying the dynamic elements.
When the ID attribute is given to an element that is unique on a webpage. It is the fastest and most reliable way to locate an element.
When the name attribute is given to an element. It may not be unique, but it is commonly used to identify elements.
Multiple elements can have the same class attribute, but it is still a reliable way to locate elements. It is used mostly in radio buttons, checkboxes, or dropdowns in case no other attribute is given.
It is useful when only one element has a particular tag name.
It is used to find an anchor element (“a” tag) with a specific link text and identify links on a webpage.
It is used to find an anchor element (“a” tag) with a partial link text and identify links with a familiar pattern on a webpage.
It uses the given locating mechanism to find the first element within the current page and return a single web element.
It also uses the given locating mechanism to find all the elements within the current page and return the list of all web elements.
It’s a command by which the current window can be closed. For example, if we have multiple browser windows open then, by using this command we can close the window with which the focus is.
This command closes all the open browser windows. For example, if we have multiple browser windows open then, this command can close all the windows at once.
isSelected() is the method used to verify if the web element is selected or not.
isEnabled() is the method used to verify if the web element is enabled or disabled within the webpage. isEnabled() is primarily used with buttons.
isDisplayed() is capable of checking for the presence of all kinds of web elements available.
WebDriver driver = new FirefoxDriver();
getText() returns the visible text of a web element, while getAttribute() returns the value of a specific attribute of the web element.
sendKeys() is the method used to type text in input boxes
driver.findElement(By.linkText(“Click here”)).click();
//Get the title of the webpage and store it in a variable.
String actualTitle = driver.getTitle();
//Type in the expected title
String expectedTitle = “ABC";
//Verify if both of them are equal
if(actualTitle.equalsIgnoreCase(expectedTitle)) {
System.out.println("Title Matched");
}
else{
System.out.println("Title didn't match");
}
No, Selenium cannot automate Captcha. The automation test engineer has to manually type the captcha while other fields can be filled automatically.
The WebElement is an interface in Selenium that is used to represent an HTML element on a web page. It provides methods to interact with the web elements, such as clicking, entering text, and getting the value. It is used to identify and manipulate the web elements in the automation testing process.
switchTo() command is used to switch between windows. Every window instantiated by the WebDriver is given a unique alphanumeric value called “Window Handle”.
To maximize the window
driver.manage().window().maximize();
We can set the timeout for a specific amount of time for all the successive web elements. In this specified time, the web driver searches for all the web elements before throwing the NoSuchElementException.
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
We can tell the Web Driver to wait for certain conditions before throwing the ElemntNotVisibleException.
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(2));
It’s a slight extension of the explicit wait. Apart from waiting for certain conditions to be met, we can also set the frequency with which we check the condition before throwing the ElementNotVisibleException.
Wait wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(TIMEOUT))
.pollingEvery(Duration.ofMillis(POLL_FREQUENCY));
In XPath, a single slash is used for creating absolute XPaths, beginning from the root node. Whereas double slash is used for creating relative XPaths.
Selenium has driver.getWindowHandles() and
driver.switchTo().window(“{windowHandleName}”)
commands to work with multiple windows.
The getWindowHandles() command returns a list of IDs corresponding to each window. If we pass a particular window handle to the
driver.switchTo().window(“{windowHandleName}”)
command then we can switch control/focus to that particular window.
for (String windowHandle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
There a multiple ways to refresh a page in Selenium-
Using the getText() method we can fetch the text over an element.
String text = driver.findElement("elementLocator").getText();
Using getAttribute(“{attributeName}”) method, we can find the value of different attributes of an element e.g.-
String valueAttribute =
driver.findElement(By.id("locator")).getAttribute("value");
All the links are of anchor tag ‘a’. So by locating elements of tagName ‘a’ we can find all the links on a webpage.
List<WebElement> links = driver.findElements(By.tagName("a"));
The 5 different exceptions you had in Selenium web drivers are
To take screenshots in Selenium, we can use the getScreenshotAs method of the TakesScreenshot interface.
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\testScreenShot.jpg"));
Using Select class-
Select countriesDropDown = new Select(driver.findElement(By.id("countries")));
There are 3 methods
dropdown.selectByVisibleText("India");
//or using the index of the option starting from 0
dropdown.selectByIndex(1);
//or using its value attribute
dropdown.selectByValue("Ind");
Using is Selected() method, we can check the state of a dropdown’s option.
Select countriesDropDown = new
Select(driver.findElement(By.id("countries")));
dropdown.selectByVisibleText("India");
//returns true or false value
System.out.println(driver.findElement(By.id("India")).isSelected());
Using the Action class, drag and drop can be performed in Selenium. Example-
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(SourceElement)
.moveToElement(TargetElement)
.release(TargetElement)
.build();
dragAndDrop.perform();
To accept or dismiss an alert box, the alert class is used. This requires first switching to the alert box and then using accept() or dismiss() command as the case may be.
Alert alert = driver.switchTo().alert();
//To accept the alert
alert.accept();
Alert alert = driver.switchTo().alert();
//To cancel the alert box
alert.dismiss();
A Selenium Maven project is a software project that uses Maven to manage the project's dependencies and build process.
Following are the navigation commands:
navigate().back() – The above command requires no parameters and takes back the user to the previous webpage in the web browser’s history.
driver.navigate().back();
navigate().forward() – This command lets the user navigate to the next web page with reference to the browser’s history.
driver.navigate().forward();
navigate().refresh() – This command lets the user refresh the current web page by reloading all the web elements.
driver.navigate().refresh();
navigate().to() – This command lets the user launch a new web browser window and navigate to the specified URL.
driver.navigate().to(“https://google.com”);
Actions class is used to hover over a web element in Selenium WebDriver Instantiate the Actions class object and use the moveToElement() method to hover over the element.
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.id("id of the element"))).build().perform();
Copyright © 2023 - Proleed Academy | All Rights Reserved.