by admin | Oct 28, 2019 | Selenium Testing, Blog, Top Picks |
As we all are aware of the fact that, Selenium 4 has not been released yet. However, three alpha versions have been released. Selenium Grid has only two components Hub and Node. On the other hand in Selenium Grid 4, we have four components namely – Router, Sessions Map, Distributor, and Node. Selenium 4 has introduced new architecture for Grid. Let’s understand how Selenium Grid 4 works with the three new processes.
Step #1-Start Sessions Map
Download selenium-server-4.0.0-alpha-2.jar & Chrome Driver in a folder and run the below command to start Sessions Map process.
java -jar selenium-server-4.0.0-alpha-2.jar sessions
What is Sessions Map? Session Map is one of the Selenium Grid Processes. Whenever a node/session is created, Node URI/Session ID gets stored in Sessions Map. Why it is getting stored here is another question. You will understand this at the last step.
Step #2-Start Distributor
Run the below command to start the distributor process.
java -jar selenium-server-4.0.0-alpha-2.jar distributor --sessions http://localhost:5556
All Selenium Grid nodes get attached under the Distributor process. If Selenium client code sends a create session request, then Distributor is responsible for assigning a node.
Step #3-Router Process
All the client requests will be received by Router. If the request is for creating a new session, then it will be redirected to the Distributor process. All other requests will be sent to the node associated with the request after querying the node URI from Sessions Map using the Session ID. Use the below command to start the router process.
java -jar selenium-server.jar router --sessions http://localhost:5556 --distributor http://localhost:5553
Step #4-Create a node
While creating a node, you do need to pass all the driver details. Drivers will get auto-detected if the exe/binary files are available in the folder where you have saved the Selenium Server jar file. Once the node is created under the distributor, the node details along with Node URI are updated in Session Map. For a thorough understanding, Please refer to the below diagram.
java -jar selenium-server-4.0.0-alpha-2.jar node --detect-drivers
Step #5-Start a remote session
Invoke the remote webdriver which will send the creation request to router. Please note – Use http://localhost:4444. Not http://localhost:4444/wd/hub.
DesiredCapabilities capability = new DesiredCapabilities();
capability.setBrowserName("chrome");
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"),capability);
Once the session is created under the node, the session ID gets updated in the Session map for the node. So that Router can easily identify the node to send the other client requests directly.
Step #6-Router sends command to node
Apart from the session creation call, all other requests will be forwarded to Node from Router directly. Each client request has a Session ID. Using the session ID, the Router queries the Node URI
and finds the node which holds the session.
In the older version of Selenium Grid, these processes occur inside the Hub process. Hence we are not aware of the process which happens at the backdrop. Though the setup is complex. Modernization will help us to troubleshoot failures and we can set up our own distributed execution elegantly.
by Mollie Brown | Feb 28, 2022 | Selenium Testing, Blog |
Even though automation is a great asset that can greatly reduce the efforts needed in software testing, it has to be used properly to reap all its benefits. There are various instances where software testing companies have tried to implement automation in their various projects and failed due to various complications. On the other hand, we’ve followed the test automation best practices in all our automation projects and have witnessed tremendous success. By doing so, we have been able to focus on aspects such as exploratory testing to unearth hidden bugs instead of focusing on repetitive tasks. So these test automation best practices can help you reduce the time needed to reach the market, increase outcome precision, avoid human error and eventually assure maximum product quality.
Decide which tests to Automate:
Similarly, it is also not recommended to automate every single test that can be automated. Apart from that, certain types of testing like exploratory testing and accessibility testing need manual efforts for it to be successful. Trying to use automation there will not result in the loss of time and effort, it will also reduce the overall quality of the product that you are testing.
So deciding which tests have to be automated plays an important role in achieving successful and efficient test automation. As a leading automation testing company, we usually follow the set of guidelines listed below for making this decision.
The type of tests that would benefit from automation.
- Repetitive tests that demand a lot of manual effort.
- Tests that are prone to human errors.
- Tests that should be executed on multiple platforms, hardware, or OS setups.
- Constant tests that will not undergo frequent changes.
- Tests that have to cover numerous builds or versions.
- Heavy tests that require numerous data sets and large amounts of data to execute.
- Tests that utilize frequently used functions.
Identify an Automation Testing Framework:
The next automation testing best practice involves identifying and choosing the right testing framework for your needs from the long list of automation testing frameworks that are available in the market. The framework you choose should be suitable for the technology that will be used to develop the app and the intended platforms that you are planning to run the tests on. It should also align with your testing strategy and goals. The cost of the automation framework is also a crucial factor to consider. The open-source options do have an edge over the commercial tools in terms of pricing and the constant improvements that happen due to various community contributors.
Pick the Right Automation Testing Tool:
Using the right automation testing tools is one of the test automation best practices as it will definitely have a huge impact on the success rate of your automation. So be clear as to what your need is and identify a list of tools that you could use and then choose the right tool for you. Since there are a lot of open-source automation tools, proper R&D will be needed before you directly implement a tool in a project without much knowledge. Here are a few factors to consider when choosing the right automation tool for your needs.
- The type of application being tested is the first focus point. If you have to test a web-based app, you can opt for Selenium for automation as it is one of the most popular web app automation testing tools. Likewise, if you are looking to test a mobile app, you can choose Appium to fulfill all your automation needs with the least effort.
- If choosing the right automation tool for your needs is crucial, so is choosing the automation tool that will suit your testing team. The most well-known languages utilized for automation testing are Java, JavaScript, Ruby, C#, and so on. Based on the language your team is comfortable with, you can pick the automation tools.
- The cost will definitely be a deciding factor as not all will have the provision to opt for commercial solutions. So you should also consider the financial plan and requirements before choosing the automation tool for your project. Selenium and -Appium are some of the best examples of open-source automation testing tools.
Execution Plan:
Once you have the right automation framework and tools, developing an effective execution plan is the next challenge. So let’s focus on the process to create an execution plan that defines all the everyday tasks and methods related to automation.
- The test cases have to be executed using pipelines like TEXTJenkins, TeamCity, Bamboo, and so on. By doing so, you will ensure that regression tests will either run as a component of a build/deployment event or at a time of your choice.
- Parallel text execution is a great approach that can enable the input from the regression tests to reach the development team as quickly as possible.
- Though you can never test a product excessively, the combination of effective test automation, solid test cases, and planned/controlled execution will assure constructive outcomes.
Allocate tasks based on skill:
Allocating tasks based on your team’s skill set is also a part and parcel of your execution plan. So allocating test cases and tasks based on their technical skill is also one of the test automation best practices. As stated earlier, 100% automation is not possible. You might have a team member who is excellent when it comes to automation. But if you assign a manual task to that person, you will be wasting a lot of potential. Likewise, the resource might have to be fluent with a certain automation tool to perform the tests. So ensure that the resource you are allocating the task to has the skills to handle it.
As a pioneer in the software testing industry, we always make sure to have a well-balanced software testing team that can handle any type of challenge. But allocating the tasks alone isn’t enough as the team members should be clear of their goals and objectives to perform as expected. So make sure that you define the need clearly to your entire team.
Release Control:
No matter how well your pipeline is developed, there will come a moment where the group needs to decide if they should deliver a build or not. Though parts of this decision-making can be automated, various parts of it will definitely require human intervention. So it goes without saying that the release control should be a seamless blend of both algorithm results and manual decision making.
- Since the outcome of the automation tests is integral to the delivery choice, you can either opt to release if all the regression tests pass or have an experienced tester endorse the outcome before release.
- Include the application logs of all the regression test cases that have been executed as a factor when deciding to release. But make sure that your regression tests have great test coverage as those logs will reveal blunders that go beyond just the UI issues.
Failure Analysis:
Finally, in this list of the test automation best practices, we will be exploring failure analysis. Since test cases are prone to failure, you must analyze the failing test cases and identify the root cause behind their failures. You should also make the necessary arrangements to resolve the detected bugs and issues. By doing so you will be able to greatly reduce the time it takes to fix the bugs once they have been identified by the testers. So by having a clear-cut cycle, you can prevent unnecessary arguments and have them focus on fixing the issues by using all their resources.
Conclusion:
We hope you have found this blog to be informative and encourage you to implement these test automation best practices in your projects as well. Though different automation projects might have their own unique challenges, these test automation best practices are a great place to start. Being a top automation testing company, we understand that it is not just being the first to the market. Rather, it is about using automation to enhance product quality.
by Chris Adams | Oct 8, 2024 | Selenium Testing, Blog |
Selenium is a free and open-source testing tool that automates website testing. It is not a single tool, but rather a set of four components known as Selenium IDE, Selenium RC, Selenium WebDriver, and Selenium Grid. Selenium offers a number of features to testers as it provides support for multiple browsers, parallel testing capabilities, and execution on multiple machines. Since it is one of the best automation solutions in the market, we, as a leading QA company have created this blog by compiling the best Selenium commands (Inclusive of Selenium 4) one would have to know to achieve successful automation.
The Advantages of Selenium WebDriver
- Selenium WebDriver is compatible with prominent programming languages such as Java, C#, PHP, Ruby, Perl, Python, and.Net.
- It supports cross-browser interoperability which allows you to test on a variety of browsers including Firefox, Chrome, Edge, and Safari.
- Tests can be run on all major operating systems such as Windows, Mac, Linux, Android, and iOS.
- Selenium WebDriver overcomes Selenium v1’s limitations such as file upload, download, pop-ups, and dialogue barriers.
The Disadvantages of Selenium WebDriver
- Despite the many disadvantages, it is not possible to generate detailed test reports.
- It is also not possible to test images.
Types of Selenium WebDriver Commands
Selenium WebDriver commands are the methods used to run Selenium test automation scripts. The Selenium WebDriver commands offer different ways to interact with the WebDriver and perform various tasks. These methods are accessed by using a driver variable and calling “driver.methodName().” Let’s first list out the different categories of the Selenium WebDriver commands and then explore how to use them in the different browsers.
Browser Initialization
So we’re starting the list of the best Selenium Commands with the one that is used to initialize a browser by helping us choose the browser we want to use for automating the script. The commonly used Selenium commands for browser initialization are,
Mozilla Firefox Syntax
System.setProperty("webdriver.gecko.driver”,”//driver- location/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
Google Chrome Syntax
System.setProperty(“webdriver.chrome.driver”,”//driver-location/chromedriver.exe”);
WebDriver driver=new ChromeDriver();
Edge Syntax
System.setProperty(“webdriver.edge.driver”,” //driver-location/edgedriver.exe”);
WebDriver driver=new EdgeDriver ();
Browser Commands
Now that the browser has been initialized, the next course of action will be to perform operations such as opening, closing, retrieving page source, and so on. So let’s take a look at the various browser commands that can be used to achieve all this.
get(String args)
The above Selenium command launches a new browser window and navigates to the specified URL. The command accepts a single string type parameter, which is usually the URL of the application under test. If you are a user of Selenium IDE, you may notice that the command is similar to the IDE open command.
Syntax:
driver.get("http://www.codoid.com");
getCurrentUrl()
As the name suggests, this Selenium command gives us the URL of the page currently loaded in the browser.
Syntax:
String url = driver.getCurrentUrl();
getTitle()
If you want to retrieve the title of the currently opened web page, you can use the above command.
Syntax:
String j = driver.getTitle();
getPageSource()
The above command will help you to get the source of the last loaded page and also to verify if a particular content is present or not by using the contains method.
Syntax:
String j=driver.getPageSource();
boolean result = driver.getPageSource().contains("String to find");
getClass()
If you’re looking to return the run time class name of the object, you can use the above command to get it done.
Syntax:
navigate().to()
It creates a new browser window with a new web page. It takes a String parameter and returns a void value.
Syntax:
driver.navigate().to(“http://google.com”);
refresh()
If you want to test by seeing how the page reacts to being refreshed, you can use the above command to refresh the current window.
Syntax:
driver.navigate().refresh();
back()
Back is a commonly used navigation action that you can achieve by using this command to go back to the previous page that you visited.
Syntax:
driver.navigate().back();
forward()
Similar to back, the forward action is also a widely used navigation action. So you can use the above command to redirect to the page you were in before clicking on the back button.
Syntax:
driver.navigate().forward ();
close()
The close() command is used to close the currently open WebDriver-controlled browser window. If the current window is the only active window in WebDriver, the browser will also be closed.
Syntax:
quit()
There is a minor distinction between the quit() and close() methods. The quit() method terminates all open browser instances, whereas the close() method terminates only the current browser instance.
Syntax:
Web Elements Commands
So now we know how to open the browser and perform various browser actions, let’s progress to the Selenium commands that can be used to identify and perform actions on WebElements like text boxes, radio buttons, checkboxes, and so on. WebElements are integral to automating test scripts.
Text Box Commands
click()
We start the list with click() as it helps us to click the textbox initially.
Syntax:
driver.findElement(By.xpath("//div//input[@id='search']")).click();
sendKeys()
Entering text in a text box is one of the basic functionalities and you can do so by using this command.
Syntax:
driver.findElement(By.xpath("//input[@id='id_q']")).sendKeys("fresher");
clear()
If you enter text, you’ll also have to clear it at times. You can clear the value in a textbox by using the above command.
Syntax:
driver.findElement(By.xpath("//input[@id='search']")).clear();
getLocation()
You can employ the above command to determine the relative position of an element on a web page. You can either use it to get a location of a particular element or to access the textbox area using the coordinates.
(i)To get the location of a particular element
org.openqa.selenium.Point location;
location=driver.findElement(By.xpath("//input[@id='search']")).getLocation();
(ii)Through org.openqa.selenium.Point we can access the textbox area using coordinates
action.moveByOffset(location.x,location.y).click().sendKeys("romeo").perform();
getSize()
If you’re in need of retrieving the height and width (i.e) the dimensions of an object, you can use the above command.
Syntax:
Dimension dimension=driver.findElement(By.id("GmailAddress")).getSize();
System.out.println("Height of webelement--->"+dimension.height);
System.out.println("Height of webelement--->"+dimension.width);
getAttribute()
You can use the above command to view the value type in a search text box.
Syntax:
StringfieldValue=driver.findElement(By.xpath(“XPATHVALUE")).getAttribute(“Attributevalue");
getText()
If you’re looking to retrieve the inner text of a particular web element that is not hidden by CSS, this command can be used.
Syntax:
System.out.print(driver.findElement(By.xpath(“XPATHVALUE”)).getText());
Radio Buttons and Check Boxes are the next web elements that we are going to see the Selenium commands for.
click()
The click() command can be used to click the radio button or check box.
Syntax:
driver.findElement(By.xpath("XPATH")).click();
isSelected()
If you’re looking to verify if they have been selected or not, you can make use of this command.
Syntax:
isDisplayed()
You can even verify if these web elements are visible or not using the above command.
Syntax:
isEnabled()
The isEnabled() command can be used to check whether the web elements are enabled or not.
Syntax:
getTagName()
You can use the above command to return the tag name of these web elements.
Syntax:
getText()
If you’d like to return the inner text, you can opt to use the above command.
Syntax:
String str=element.getText();
The Dropdown Box and Button Commands can be used to perform operations on the Dropdown so that we’ll be able to automate the process of selecting the required field from the dropdown.
Select class
The select class can be used to work with the dropdown elements as you’ll be able to choose the option using the various Selenium commands.
Syntax:
Select sel=new Select(driver.findElement(By.xpath("XPATHVALUE")));
sel.selectByIndex(INDEX_VALUE);
sel.selectByValue("VALUE");
sel.selectByVisibleText("VISIBLE_TEXT_VALUE");
getOptions()
If you would like to know the number of options in a dropdown, you can use this command.
Syntax:
int count=sel.getOptions().size();
getFirstSelectedOption()
You can return the first selected value using the above command.
Syntax:
String str=sel.getFirstSelectedOption().getText();
getAllSelectedOptions()
But, if you want to return a number of selected options, you’ll have to use this option.
Syntax:
int count=sel.getAllSelectedOptions().size();
isMultiple()
But what if the dropdown isn’t made for multiple options? So the isMultiple() command will come in handy as it returns true if the element supports multiple selecting options at the same time.
Syntax:
Boolean boolean=sel.isMultiple();
Deselect the options
You can even deselect the options you have selected using the above command.
Syntax:
sel.deselectAll();
sel.deselectByIndex(INDEX_VALUE);
sel.deselectByValue("VALUE");
sel.deselectByVisibleText("VISIBLE_TEXT_VALUE");
click()
The click() command can be used to click the element.
Syntax:
driver.findElement(By.xpath("XPATHVALUE")).click();
submit()
Finally, you should be able to automate the action of submitting the form using this command.
Syntax:
driver.findElement(By.xpath("XPATHVALUE")).submit();
Windows Commands
So the next step in attaining effective automation would be to automate the actions across different windows of the browser. So let’s find out how to switch to another window, pass the driver instance to another window.
Note: In order to switch to another window, we have to first identify the tab we want to switch to.
Syntax:
driver.switchTo().window(driver.getWindowHandles().toArray()[].toString();
getWindowHandle()
You can obtain the name of the current window by using this command as it will return the alphanumeric name of the current window.
Syntax:
String handle= driver.getWindowHandle();
getWindowHandles()
If there are multiple windows open and you want to retrieve the names of numerous window handles, you can utilize this command.
Syntax:
Set<String> handle= driver.getWindowHandles();
size()
You can even find out how many windows are currently open using this command.
Syntax:
int count=driver.getWindowHandles().size();
alert()
The above command can be used to switch to the alert window.
Syntax:
driver.switchTo().alert();
accept()
You can use this command with the previous command to switch to the alert window and accept the alert.
Syntax:
driver.switchTo().alert().accept();
sendKeys(String stringToSend)
If you’re looking to enter a specified string pattern into the alert box, you can use this command.
Syntax:
driver.switchTo().alert().sendkeys(String);
getText()
You can even retrieve the string content of the alert by using the above Selenium command.
Syntax:
String str=driver.switchTo().alert().getText();
ScreenShot
Taking a screenshot is a crucial part of the automation process and you can achieve it by using this command.
Syntax:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
Frames Commands
Frames Commands are those that can be used to perform operations on the frames as it helps us to switch from one frame to another and also perform actions on the particular frames.
switchTo()
As the name suggests, you can use the above command to switch to another frame.
Syntax:
river.switchTo().frame(FRAME_INDEX);
driver.switchTo().frame(FRAME_NAME);
defaultContent()
If you would like to go back to the current window from any frame, you can employ this command.
Syntax:
driver.switchTo().defaultContent();
parentFrame()
When it comes to switching to the parent frame, you can use the above command.
Syntax:
driver.switchTo().parentFrame();
Iframe
The above command can be used to switch to an iframe.
Syntax:
driver.switchTo().frame(driver.findElements(By.tagName(“iframe”).get(FRAME_INDEX));
Actions Commands
In general, the methods of the actions classes are divided into two categories:
- Mouse-Controlled Actions
- Actions on the Keyboard
The Actions class has a number of methods that will return an action object unless otherwise specified. Mimicking a real user’s actions is impossible to do without automating the mouse and keyboard actions. So let’s take a look at how to do it.
build()
The build() is a very important command that can be used to generate a chain of actions that you want to perform.
Syntax:
Actions action = new Actions(driver);
WebElement e= webdriver.findElement(By.linkText(“XPATH"));
action.moveToElement(e).moveToElement(driver.findElement(By.xpath(“XPATHVALUE"))).click().build().perform();
click()
This command is used to click at the current mouse location.
Syntax:
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
clickAndHold()
If you would like to click and hold on to it at the current mouse location, you can do so using this command.
Syntax:
contextClick(WebElement onElement)
Context-click is nothing but clicking the right mouse button at the current location.
Syntax:
Actions action= new Actions(driver);
action.contextClick(productLink).build().perform();
release()
If you hold on to the click, you’d have to release it at some point and you can do so by using this command as it releases the depressed left mouse button at the current mouse location.
Syntax:
Actions builder = new Actions(driver);
WebElement canvas = driver.findElement(By.id("tutorial"));
Action dragAndDrop = builder.clickAndHold(canvas).moveByOffset(100, 150).release(canvas).build().perform();
doubleClick()
Another common mouse action is the double-clicking process and you can perform that by using this command.
Syntax:
Actions builder = new Actions(driver);
WebElement canvas = driver.findElement(By.id("tutorial"));
Action dragAndDrop = builder.clickAndHold(canvas).moveByOffset(100, 150).release(canvas).build().perform();
dragAndDrop(WebElement source, WebElement target)
Drag and drop is performed by clicking and holding the source element and then moving to the target location to release it. You can achieve this by using the above command.
Syntax:
Actions action= new Actions(driver);
WebElement Source=driver.findElement(By.id("draggable"));
WebElement Target=driver.findElement(By.id("droppable"));
act.dragAndDrop(Source, Target).build().perform();
dragAndDropBy(WebElement source, int xOffset, int yOffset)
This command is similar to the regular drag and drop, but it differs in the way that the movement happens with reference to a defined offset.
Syntax:
act.dragAndDropBy(From, 140, 18).perform();
here 140,8 are the x and y offset of target element.
moveByOffset(int xOffset, int yOffset)
You can offset the mouse’s position by either keeping the current position or (0,0) as the reference.
Syntax:
Actions builder = new Actions(driver);
WebElement canvas = driver.findElement(By.id("tutorial"));
Action dragAndDrop = builder.clickAndHold(canvas).moveByOffset(100, 150).release(canvas).build().perform();
moveToElement(WebElement toElement)
You can even move the mouse to the middle of a web element by using this command.
Syntax:
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("XPATHVALUE").click().build().perform();
moveToElement(WebElement toElement, int xOffset, int yOffset)
You can use the above command to move the mouse to an offset from the top-left corner of the element.
Syntax:
Actions builder = new Actions(driver);
builder.moveToElement(knownElement, 10, 25).click().build().perform();
perform()
You can perform the actions without having to call the build() command first.
Syntax:
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
keyDown(),keyUp()
The above Selenium commands can be used to perform single key presses and key releases.
Syntax:
Actions action = new Actions(driver);
action.keyDown(Keys.control).sendKeys("a").keyUp(Keys.control).
sendKeys(Keys.DELETE).perform();
Synchronization Commands
We’re almost done with all the respective Selenium commands that one will need to complete their automation. Imagine an action such as a page reloads or a form was submitted, the script would have to wait for a certain amount of time to ensure the action has been completed. That is where the Selenium commands for Synchronization come into play.
Thread.sleep():
This command is used to pause for a defined time. Time is defined in milliseconds for this method.
Syntax:
implicitlyWait()
By using the above command, the script execution will wait for a specified amount of time before it moves on to the next step.
Syntax:
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
ExplicitWait
But since setting a specified amount of time for every single command is not that efficient, you can make use of this command to be adaptable by waiting for certain conditions to be satisfied. This can be achieved by using different types of ExpectedConditions.
Syntax:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElementele=wait.until(ExpectedConditions.elementToBeClickable(By.id(“XPATH")));
Note: WebDriver Wait statements can be very useful for effective scripting as we can avoid using the Thread.sleep() Selenium commands.
Now let’s explore the various expected conditions.
visibilityOfElementLocated()
You can wait until an element to be located becomes visible using this command.
Syntax:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“XPATH VALUE"));
elementToBeClickable()
This command can be used to wait for an element to become visible and clickable.
Syntax:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(“/XPATH VALUE”)));
textToBePresentInElement()
By using this command, you’ll be able to make the execution wait until an element has a certain string pattern.
Syntax:
wait.until(ExpectedConditions.textToBePresentInElement(By.xpath( XPATH VALUE”), “text to be found”));
alertIsPresent()
If you want the execution to wait until an alert box appears, you can use this command.
Syntax:
wait.until(ExpectedConditions.alertIsPresent()) !=null);
titleIs()
You can even wait for a page with a specific title using this command.
Syntax:
wait.until(ExpectedConditions.titleIs(“gmail”));
frameToBeAvailableAndSwitchToIt()
The above command can be used to wait for a frame to become available and for the control to switch automatically.
Syntax:
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id(“XPATH VALUE”)));
FluentWait()
This command allows you to control two important aspects
(i) The maximum amount of time to wait for a condition to be satisfied along with the frequency at which to check if the condition has been satisfied or not.
(ii) You can even configure the command to ignore specific types of exceptions whilst waiting.
Syntax:
Wait wait = new FluentWait(driver);
withTimeout(30, SECONDS);
pollingEvery(5, SECONDS);
ignoring(NoSuchElementException.class);
Selenium 4 Commands
So all the above Selenium commands that we saw were from Selenium 3. Now, with the launch of Selenium 4, some more commands have been added and some changes have also been implemented to the Selenium 3 commands. Since we are focused on providing the best Selenium testing services to all our clients, we are always on a path of continuous learning. So let’s take a look at all the major changes one by one.
Relative Locators
There were no techniques (or particular methods) in Selenium 3 to locate Web Elements relative to the surrounding components. But relative locators have been introduced in Selenium 4 to make it easier to locate Web Elements based on their visual placement on the UI relative to other DOM elements.
above()
This command is used to identify the required web element that is just above the specified element.
Syntax:
WebElement ele1;
ele1 = driver.findElement(By.id("123"));
driver.findElement(withTagName("ABC").above(ele1)).getText();
below()
Likewise, to identify the required web element just below the specified element, the above command can be used.
Syntax:
WebElement ele1;
ele1 = driver.findElement(By.id("123"));
String ele2 = driver.findElement(withTagName("AYZ").below(ele1)).getText();
toLeftOf()
Similarly, this command can be used to identify the required web element to the left of the specified element.
Syntax:
driver.findElement( withTagName("ABC").toLeftOf(ele1)).getText();
toRightOf()
Finally, the above command can be used to identify the required web element to the right of the specified element.
Syntax:
driver.findElement( withTagName("ABC"). toRightOf (ele1)).getText();
near()
You can even identify required web elements that are about mexa piles away from the specified element.
Syntax:
driver.findElement( withTagName("ABC"). near(ele1)).getText();
Window and Tab management
It is very common for a link to open in a new tab or window based on the link you click or how you open it. We had to use the switch operation and the windowHandle function in Selenium 3 to handle it. But Selenium 4 now includes a new API called newWindow, which allows users to create and switch between new windows/tabs without having to build a new WebDriver object.
Syntax:
driver.get("https://www.flipkart.com/");
driver.switchTo().newWindow(WindowType.WINDOW);
driver.navigate().to("https://www.codoid.com/");
driver.get("https://www.flipkart.com/");
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to("https://www.codoid.com/");
Conclusion:
We hope you have found our comprehensive list of all the prominent Selenium Commands and their syntaxes to be useful. We have also made sure to add the commands from Selenium 4 to make this a conclusive blog that captures all the needs. These are the Selenium Commands that we use in our automaton testing scripts and hope you’ll use them in your scripts too.
by admin | Feb 11, 2022 | Selenium Testing, Blog, Latest Post |
Over the years, Selenium test automation has become an integral part of quality assurance. Despite its immense popularity, many might have faced various difficulties in the form of flaky and unstable tests. But does that imply that the issue is with Selenium? Being a leading test automation services provider, we have no second thoughts in its capacity and can firmly say that everybody can get the best out of it if they follow the best practices. Though there are no specific rules for creating your Selenium test automation scripts, these Selenium best practices will definitely act as effective guidelines. So in this blog, we have listed the Selenium best practices that one can follow to enhance the efficiency of their Selenium Test Automation.
Know Which Tests Should Be Automated
It is a well-known fact that 100% automation isn’t possible. So trying to automate every single scenario or automating every scenario that can be automated is not the best approach. So before you kickstart your automated browser testing, make sure you are clear of what tests have to be automated. When it comes to automation, always keep in mind that just because you can do something doesn’t mean that you should do it.
So start off by designing effective test cases before you start working on automation. Apart from that, analyzing if automating a scenario will result in the reduction of repetitive and tedious tasks is also crucial. So if you automate a test case that doesn’t have much frequency of occurrence, then spending that much time and effort on automating the test case is not validated. So as a first step, look for static test cases that have predictable outputs.
Choose the Best Locators
Since Selenium has a wide range of web locators available, choosing the best locators can impact the quality of your Selenium test automation. Some of the most often used web locators in Selenium WebDriver are
- ID
- Class
- tagName
- Name
- Link text
- XPath
- CSS Selector
- DOM Locator, and so on.
It’s important to pick the proper one as it will reduce the impact of user interface changes during testing. For example, if there is a dynamic scenario, link text will generally be preferable. But if you are looking for web locators that are less brittle, ID, Class, or Name would be the way to go even though it isn’t that user-friendly.
To learn more about the many ways to locate web elements and to know which locators are most suited for specific scenarios, read our blog about the different XPath methods in Selenium.
Utilize time-efficient approaches
It is not always about getting the script to work as your test scripts should also be efficient when executed. What might feel like just a few seconds will definitely have huge impacts when you have to execute the tests numerous times in different combinations. That is why we have incorporated the following Selenium best practices that will help you save loads of time.
Use Selenium Wait Commands Instead of Thread.sleep()
Various factors like network slowness or server issues can cause your web application to take additional time to load. So your automation scripts should be able to wait for all the elements to load before starting the test execution.
The Thread.sleep() method can be used for this purpose as it can pause your script for a specific amount of time.
Sleep
Sleep() waits for the number of seconds supplied in the brackets, regardless of whether the working page is ready.
There is a major flaw with this function as the script will wait for a fixed time for every scenario irrespective of the need. For example, let’s assume that you have set the sleep time to be 6 seconds. But what if the webpage that has to be tested loads in just 3 seconds? Or what if the webpage took 7 seconds to load? You will either be wasting time or getting inaccurate results because of this approach. But you can prevent such issues by utilizing implicit or explicit waits.
Wait
Explicit: The code execution is halted until a certain condition is satisfied.
Packages to import to use the Explicit wait
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
Code to initialize a Wait Object using WebDriverWait Class
WebDriverWait wait = new WebDriverWait(driver,10);
Here WebDriverWait is the class, and new is the reference variable. It will either for the defined amount or for the expected condition to be fulfilled. This is just a sample and you can use the same concept with the other available expected conditions. Though there are various options, we have listed a few below,
- visibilityOf()
- alertIsPresent()
- titleContains()
- textToBePresentInElement()
- invisibilityOfTheElementLocated()
- elementSelectionStateToBe()
- elementToBeClickable()
- presenceOfAllElementsLocatedBy()
- frameToBeAvaliableAndSwitchToIt()
Example:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),CODOID)]")));
By now you would have understood why we have included the usage of the wait commands in our list of Selenium best practices. Beyond the explicit wait, there are also implicit and fluent wait commands that give you more options. So let’s explore them now.
Implicit: The WebDriver is advised to keep polling the DOM until the element search is complete. By default, the time is set to 0.
Syntax
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Fluent: It waits for a specified maximum amount of time for a specific condition. What makes it different is that it will validate the element at regular intervals before throwing “ElementNotVisibleException”.
Syntax
Wait wait = new FluentWait(Webdriver_Reference)
.withTimout(timeOut, SECONDS)
.pollingEvery(timeOut, SECONDS)
.Ignoring(Exception class)
Sample Snippet
We have mentioned a sampe snippet that will wait for 30 seconds and check the condition every 5 seconds.
FluentWait wait = new FluentWait(driver);
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(new Function<WebDriver, WebElement) {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("element"));
}
});
Implicit: The WebDriver is advised to keep polling the DOM until the element search is complete. By default, the time is set to 0.
Use Assert and Verify-in Appropriate Scenarios
Let’s assume that there is a task to transport 100 boxes of apples to the market. But what if all the boxes of apples are rotten already? Will there be any use in putting all the effort and time to get the apple to the market only for it to go unsold? Likewise, there will be various scenarios where there are hard fails that will compromise the rest of the execution. Please keep in mind that Asserts should only be used if you want to stop the test execution when a hard failure occurs. If the assert condition is false, the execution will come to a halt, and no more tests will be run. So it is important to identify such bottlenecks and ensure that you use Asserts to save time.
If we take an e-commerce site as an example, such scenarios might come at the very beginning where a user is unable to login, or even in between where the user isn’t able to add any product to their cart. If these functionalities fail, there will be no point in testing the search functionality or payment process as it would all fail due to their dependency.
If the errors are not that critical, then you will be able to run the tests irrespective of the verify’s condition.
Increase Test Coverage
There is just no easy workaround when it comes to testing a website or an application across a variety of devices and browsers. The devices are further split based on the operating system they use as well. So let’s take a look at a few Selenium Best Practices that will help you increase the test coverage.
Browser Compatibility Matrix
Just like knowing what to automate, it is important to figure out the high-priority combinations that are used by your target audience. So create a Browser Compatibility Matrix by performing a product analysis to obtain detailed insights. Trying to perform automation testing with Selenium without narrowing down your target will not only be a cumbersome task, but it will also be a waste of time and resources.
Make use of Parallel Testing in Selenium
Though you are able to narrow down the device combinations, you will not always have the time to test everything one by one. Since Selenium’s parallel testing option is a major reason for its popularity, you can employ it not just to save time, but to also improve your test coverage in an efficient way. Almost all major test frameworks, such as PyTest, PyUnit, TestNG, Cucumber, and so on support simultaneous testing on a Selenium Grid. If you are not familiar with the concept of creating a Selenium Grid for distributed execution, make sure to read our conclusive blog.
Being the innovators we are, we even developed an open-source tool called VisGrid that will provide you with a UI to easily start a Selenium Grid, create & attach nodes, and so on.
Use many driver implementations instead of a single one
Since Selenium’s WebDrivers are not interchangeable, executing automated cross-browser tests on a local computer is not the same as running them on a continuous build server. It would be a mistake to assume that the following test will utilize Firefox WebDriver in that situation (or Chrome WebDriver or any other WebDriver).
When running integration tests in a continuous build environment, the test will only get a RemoteDriver object (i.e., WebDriver for any target browser). Using parameter notes to manage multiple browser types and prepare the code ready for simultaneous execution is one of the Selenium best practices (or parallel testing). LabelledParameterized (@Parameters in TestNG and @RunWith in JUnit) may be used to build small frameworks in Selenium.
Ease Code Maintenance
When working on automation tests with Selenium, it’s critical to pay attention to the test code’s maintainability and reusability. As one of the best test automation companies, we always make sure our scripts are easy to maintain by implementing the following Selenium best practices.
Use design principles and patterns, such as the Page Object Model
With the ever-increasing client expectations, a website’s user interface (UI) will inevitably change as new updates get introduced at regular intervals. So the locators for certain UI components will vary as well. This implies that QAs have to once again create new test cases for the same page or make numerous changes at various places of the script, which can be time-consuming.
So you can employ the Page Object Model design pattern to create test scripts and overcome this issue. In this approach, each web page is considered a class file in the design pattern, and each class file contains related web components.
This method greatly increases the reusability of the code while also making test maintenance easier. Read our complete guide to page object model in Selenium before you implement it.
Quarantine flaky tests
If a test keeps failing at random even if the code hasn’t gone through any changes, then it is called a flaky test. Since the testers can never be sure of the results, they will lose trust in the code and end up re-running the scripts, or debugging the scripts even though there might not be an actual defect. So make sure to quarantine such tests from your other tests. Once they are separated, make sure to analyze them and fix them before you bring them back to your test suite as flaky tests can disrupt your entire automation test suite with false positives and negatives.
Wrap Selenium Calls
You might be using various web locators such as XPath and ID to locate different web elements on the page. So the code that is regularly used in the implementation should be separated into its own API to reduce code duplication. By following this approach, you will be able to reduce the code size and enhance maintainability.
Use a standardized directory structure
Page Objects, Helper functions, and file(s) that include web locator information needed in test scenarios may all be found in the Src folder. The actual test implementation can be stored in the test folder.
When it comes to a directory structure for Selenium test automation, we don’t have a common guideline. Selenium best practices, on the other hand, proposes that we have a directory structure that isolates the test implementation from the test automation framework. This improves the test code’s structure.
Implement Data-Driven Testing for Parameterization
Hard coding test values in test automation scripts are extremely troublesome and not scalable in any way. But you can avoid the various disadvantages like code duplication by implementing data-driven testing by using Parameterization. It aids in the execution of test cases that have a variety of inputs (or data sets).
Naming Convention
Improper naming of your web elements in your automation script could lead to a lot of unnecessary confusion. You may not face any issues as long as you are the only person using the script, but you have to ensure that your script can be used by anybody. Effective naming eases code maintenance by also making it easier to find the web element you are looking for in your code. Now let’s see a few tips that can help you.
1. Add prefixes to the name of your web elements as shown in the below table as it describes the type of web elements being used. In addition to that, you can name the web element based on how it appears in the UI.
S. No | Web Element Type | Prefix | Example |
1 | Text Box | Txt | TxtUsername,TxtPassword |
2 | Button | Btn | BtnSignUp , BtnLogin |
3 | Drop Down | Dd | DdCountry , DdYear |
4 | Select Drop Down | Sdd | SddMonth , SddYear |
5 | Check Box | Chk | ChkGender, ChkLanguage |
6 | Header | Hdr | HdrName, HdrUser |
7 | Table | Tbl | TblBooks, TblProducts |
8 | Label | Lbl | LblUserName, LblPassword |
9 | Image | Img | ImgProfile, ImgCart |
2. You can even add prefixes that denote the action to be performed on the web element.
Example:
S. No | Action | Prefix | Example |
1 | Click | Clk | ClkLogin, ClkIcon |
2 | Type | Set | SetMobileNo, SetPassword |
3 | Verify | Verify | VerifyLogo |
4 | Check A Check Box | Chk | ChkLanguage |
5 | Select Value From Drop Down | Select | SelectCountry, SelectZone |
3. Likewise, you can make use of suffixes when creating action pages.
Example:
SignUpPage
LoginPage
4. To make it even clear, you can use a camel case to name the web elements.
Camel case is nothing but a typing convention like lower case and upper case in which the first letter of every word is capitalized.
Example:
If a text box has the label as “Email address”, name it as “txtEmailAddress”
Proper Documentation
Creating proper documentation for your automation tests in Selenium is as important as any of the other Selenium best practices we have discussed thus far.
Use Automated Screenshots to Analyse Failures
Automation Test Script running into failures is an eventuality. But we will not be sure if the failure is due to a defect in the Application Under Test (AUT) or if there is a problem with our code. So analyzing the cause of a test failure will be very crucial, and so taking screenshots is a best practice that can save a lot of time. Screenshots are not just great for failures as you can make use of screenshots as evidence for success.
To capture a screenshot, use the getScreenshotAs function in the following code sample.
File source = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File("Test Screenshot.png"));
Log and Report
Finding that one failed test case in a large test suite is like finding a needle in a haystack. So using console logs at suitable points in the test code helps us understand the issues better and zero in on the core problem.
Debug, info, warning, error, and critical are some of the common log levels that are available in most programming languages. Since adding unnecessary logs to the test implementation might cause test execution to be delayed, it is better to add logs with level errors in scenarios that enable easier tracking.
Like logging, reporting is also an important aspect of Selenium test automation as it helps determine if the test has passed or failed. You can also keep track of the progress of test suites (or test cases). This in turn helps improve the readability of the output and helps you reduce the amount of time spent maintaining test data as you can easily analyze the features and test coverage.
Set Zoom Level to 100% and Maximize the Window
Make sure to set the zoom level in your browser to 100% during your automation as it is one of the basics that should be followed regardless of which browser you opt to use. It will both provide a native mouse experience and ensure that the native mouse events are assigned to the proper coordinates.
Ensure to maximize the window as soon as the test URL is loaded as Selenium doesn’t launch the browser in the maximized mode by default.
Snippet
driver.manage().window().maximize();
Conclusion
We have seen great results by implementing these Selenium best practices in our automation testing projects and hope you will them useful in the future as well. We had listed various Selenium best practices under particular categories to enable a crystal clear understanding of how each of them impacts the testing process on the whole. So we hope you have enjoyed reading our blog and found the content to be informative as well.
by admin | Dec 7, 2021 | Selenium Testing, Blog, Latest Post |
Object locating technique is one of the fundamental when it comes to automation testing. If we test the application manually, we would interact with various objects or elements like links, buttons, radio buttons, lists, text boxes, sliders, and so on. So for us to mimic an actual user, the automation tool would also have to perform the same actions, and locating the objects that have to interact with is the first goal. Once the object is located, we can perform the desired actions like clicking on it, entering text, and so on. Selenium is one of the prominent automation testing tools and it uses the concept of ‘Locators’ to identify the elements/objects. So let’s take a look at the Different XPath Methods in Selenium as they are the building blocks of any Selenium automation script.
Steps to find elements in DOM:-
Before we head over to explore the Different XPath Methods in Selenium, let’s also look into a few basics like the ways to find elements in DOM and the different locators that exist in Selenium.
1. First and foremost, you have to open the website that you want to automate. Once it has loaded, click on the ‘F12’ key to open DevTools. As shown in the below image, click on the inspect icon to enable it and then click on the element you want to inspect.
2. There are also other ways to open the developer’s tools. You can either click on the ‘Menu’ option in the browser, click on ‘More Tools’ under that, and then click on the ‘Developer tools’ option that is shown.
3. If not you can just right-click on the element you want to inspect and click on the ‘Inspect’ option to locate the element.
Different Types of Locators in Selenium:-
Now that we know the ways to open Developer tools in our browser, let’s take a look at the different types of locators that can be used to identify the various web elements.
- ID
- Name
- LinkText
- Partial LinkText
- CSS Selector
- Xpath
ID Locator:-
ID is the first choice for many testers as it is one of the safest and fastest locators that can be used to find the location of an element. What makes ID so effective is that it is unique for each element on the web page, making it easily identifiable. IDs are mostly account numbers, university roll numbers, usernames, passwords, and so on as they will be unique.
Name Locator:-
If you want to locate an element that doesn’t have too many elements with similar names, you could use the name locator as it is also an effective way to locate an element. If we take a form to be an example, you might have various text fields and one submit button. So locating text fields using the names can be hard. But since there is only one submit button you will be able to locate it without much confusion. If at all there are two or more elements with the same name attribute, then the first element with the value of the name attribute will be returned. If at all there are no elements that have a matching name attribute, then a NoSuchElementException will be raised.
LinkText:-
Of the Different Types of Locators in Selenium, there are two locators that can be used to locate hyperlinks on a webpage. So let’s say you want to verify if a link is working correctly on a page. You can make use of linkText to locate that hyperlink by using the exact match of the string.
Partial LinkText:-
The main distinction from the LinkText in Selenium to the Partial LinkText is that it doesn’t investigate the specific match of the string. So it will even be able to identify fractional matches. This will come in handy when you have to search hyperlinks that have a high text length.
CSS Selector:-
What if you have to locate complex elements on the page? The CSS selector will be the best choice for you. The only downside with CSS identification is that it is not supported by all browsers. But it may not be a huge concern as it is supported by all the widely used browsers like Google Chrome, Mozilla Firefox, Safari, Opera, and so on.
Xpath:-
XPath stands for XML path language. It is based on the tree model of XML documents and provides the ability to navigate around the tree by selecting nodes using a variety of criteria. Though XPath locators are reliable, they are slower in comparison to the CSS Selectors. XPath is also supported by all the major web browsers. Now, let’s take a look at the different XPath methods and their syntaxes.
The Different XPath Methods in Selenium:-
basic xpath – To select a node based on the attribute,
Syntax:
//tagname[@attribute =' value']
contains – We can use partial text to find an element,
Syntax:
//tagname[contains(@attribute,'value')]
using OR – The element will be located if any one condition or both conditions are true.
Syntax:
//tagname[@attribute='value' or @attribute ='value']
using AND – The element will be located only if both the conditions are true,
Syntax:
//tagname[@attribute='value' and @attribute ='value']
starts-with – This can be used in a webpage where the attribute value changes on refreshing the page or due to dynamic operation on the page as it will locate the element with the starting text alone,
Syntax:
//tagname[starts-with(@attribute,'value')]
text() – It is an inbuilt function used to locate the element based on the text of the element,
Syntax:
axes – Can be used to locate elements that are more complex and dynamic by following various methods like following, parent, child, and so on.
Locating Techniques:-
Knowing the Different XPath Methods in Selenium is the first step, but employing them in an effective manner to get the job done is the key. As a leading automation testing company, we make use of some of the best locating techniques that have made our job easy. So let’s explore them now.
1) To find an element using the last and position keywords
//input[@type='text'][position()=1 or position()=last()]
We can get the first and last element inside a div tag using the position and last keywords.
2) To locate an element with the keyword count
We can locate an element by the count of another element. In this example, we have located the div tag by the count of its child input tags (i.e.) 2.
3) To identify an element with the parent keyword
//input[@id='name_id']/parent::div
Since we can locate an element with the element of the “parent” keyword, we have located the div tag which is the parent of the input tag.
4) To locate an element with the following- sibling keyword
//input/following-sibling::input[@name='Gender']
In this example, we have three input tags under a parent tag div. If you want to locate only one input tag among the three tags, we can use the following-sibling keyword to do so.
5) To identify with a single class attribute
Consider a situation where there are 4 input tags and that one input tag has a class attribute. In this case, if all the other elements for the input tags are the same, we can locate the element with the help of the Class attribute.
//div[@class='content_2']//input[@class]
6) Odd & Even Rows in a Table
We can even locate the elements in an odd and even row of a table by using the below commands. It will work like a filter as we will get all the elements in the even/odd rows as the result.
ODD ==> table[id=table1] td:nth-child(2n+1)
Even ==> table[id=table1] td:nth-child(2n+2)
7) List of elements in a table
If you’re looking to locate a list of elements in a table by sorting them in ascending or descending orders, we can use the below technique.
//table[@id='table1']//td[3]
8) The Indexing Technique
If you want to select an element inside the table, you can use this indexing technique to locate the particular element.
//table[@id='table1']//td[1]//following::td[text()='Sam']
9) To locate elements based on the value
We have used an example where all the elements that have a price that is less than or equal to 2 are located.
10) To verify if a checkbox has been checked or not
Say you want to pick out elements that have not yet been checked in a list of elements, you can use the below technique to isolate the type of elements you want.
input#good:checked
input#good:not(:checked)
Here, input - tag name, good - id
Conclusion
We hope you found this blog to be informative as knowing the Different XPath Methods in Selenium is one of the must-know basics of automation testing. As a leading QA company, we are always keen on the knowledge and the positive mindset that is required to create swift solutions to big problems. So gaining an in-depth understanding of the various techniques will play an instrumental role in your growth as an automation test engineer. If you are not clear, then you will have a tough time figuring out quick solutions for your automation needs.
by admin | Nov 30, 2021 | Selenium Testing, Blog, Latest Post |
Now that Selenium 4 has been officially released, we thought it’ll be a great idea to compare the architecture of both Selenium 3 & 4 and explore the Selenium 4 New Features. As one of the leading automation testing companies, we have been using Selenium for years and years over numerous projects to deliver high-quality Automation Testing Services to our clients. We feel that the upgrades in Selenium 4 are all welcome features that will be useful in helping the software testing community. Now let’s kick start this blog by comparing the two architectures.
Selenium 3 Architecture:
- The architecture for Selenium 3 includes the JSON Wire Protocol (JSON stands for JavaScript Object Notation).
- The JSON Wire Protocol operates as a mediator to transfer information from the client to the server over HTTP (HyperText Transfer Protocol).
- The Request is sent from the Selenium Client & the WebDriver Language Bindings module and received by the JSON Wire Protocol over HTTP. It is then secured by the Browser Driver module as shown in the above figure.
- Therefore the request command is delivered to a Web Browser module where the real automation takes place as shown in the above figure.
Selenium 4 Architecture:
- Selenium 3 includes the JSON Wire Protocol as discussed above, whereas Selenium 4 has introduced the W3C protocol as the communication medium of the browser between the client and server.
- Here, the client has two parts called the Selenium Client and WebDriver Language Bindings. The Browser Drivers acts as the server as shown in the above figure.
- The Selenium Client sends out a request to perform a command.
- The Browser Drivers receives that request and then returns a response after an automation test script is executed on the web browser.
Selenium 4 New Features:
Now that we understand how both versions work, let’s explore what are the new features that Selenium 4 has to offer.
1. Selenium WebDriver:
It is an open-source API that the web driver allows to interact with the browser just like a real user.
2. Selenium IDE:
It enables us to record and playback the user interactions with the browser. The new version of Selenium IDE is now available with more features.
3. Selenium Grid:
It can be used to save time by distributing and running tests on multiple browsers, operating systems, and machines.
4. Using WebDriver Manager:
Prior to the release of Selenium 4, if you wanted to run Selenium WebDriver automation scripts in Chrome, you had to first download chromedriver.exe and then use the System.setProperty as shown below to set its path.
System.setProperty("webdriver.chrome.driver","/absolute/path/to/binary/chromedriver");
After Selenium 4 release
Step 1: Add the Maven Dependency for the WebDriver Manager
https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager
Step 2: Add the code
Instead of the “System.setProperty()” call, the class code for WebDriverManager should be used.
WebDriverManager.chromedriver().setup();
Step 3: We can also choose a specific version of the browser as shown below.
WebDriverManager.chromedriver().driverVersion(“92.0”).setup();
5. To Capture screenshot of specific web element in selenium 4:
Users being able to take a screenshot of a specific web element is another one of the best Selenium 4 New Features that can make life easier for testers. Earlier, it was possible to take a screenshot of the entire page, but there was no provision to take the screenshot of a specific web element. Though it is a simple upgrade, it can come in very handy during real-world usage.
WebElement logo =driver.findElement(By.xpath("//div[@class='logo_container']"));
File file=logo.getScreenshotAs(OutputType.FILE);
File destFile =new File(“codoid.png”);
FileUtils.copyFile(file,destfile);
6. To open the new tab on the browser in Selenium 4:
The users can save a lot of time as it is now possible to open a URL in a new tab along with the existing tab. Let’s take a look at the sample code for opening two URLs in two different tabs.
public class SampleTab {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","src/Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to("https://codoid.com/");
}
}
7. To Open a new window on the browser in Selenium 4:
So it doesn’t just stop with new tabs, you can also open a new window on the same browser and access two applications at the same time in Selenium 4.
public class SampleWindow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","src/Drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.switchTo().newWindow(WindowType.WINDOW);
driver.navigate().to("https://codoid.com/");
}
}
8. Relative Locators:
Selenium 4 has even added 5 relative locators that enable testers to find web elements relative to another element in the implemented DOM.
The Five locators newly added to Selenium 4:
- toLeftOf()
- toRightOf()
- above()
- below()
- near()
The Key Differences between Selenium 3 and Selenium 4
S. No | Selenium 3 | Selenium 4 |
1 | The Chrome Driver Extends Directly To The Remote Web Driver. | The Chrome Driver Class Extends To The Chromium Driver That Has A Few Predefined Ways To Access The Dev Tool. |
2 | The JSON Wire Protocol Operates As A Mediator To Transfer Information From The Client To The Server Over HTTP. | Since It Uses The W3C (World Wide Web Consortium) Standard Protocol, The Driver And Browser Communication Follows The Standard Procedure. |
3 | The Testers Always Have To Start The Hub And Node Jars Every Time They Perform Automation Testing. | Testers Needn’t Have To Start The Hub And Node Jars Every Time They Perform Automation Testing. |
Conclusion
So it is very evident that the Selenium 4 upgrade has a lot of good improvements to offer including better documentation. By seeing the architectural changes, we hope you will be in a better position to use the tool more effectively. As a leading QA company, we always make it a point to know the tool inside and out as it helps us utilize the full potential of the tool.