whatsapp

Selenium Advanced level Tutorial: Learn In Detail

Selenium

Monday April 8, 2019

Now, you know almost the stuff which is required for selenium intermediate and beginner level. Now, you are proficient enough to deal with all the advanced level of selenium web driver. You can then practice those and while you are done with those, you can move forward to the advanced level of course.

Let’s see what lies ahead and what can give you an edge in your interviews. You can look at this advanced course so that you can be ahead of all the candidates who just know the basic and intermediate selenium course. Let’s have a look at Selenium Advanced level Tutorial

Selenium Grid

Selenium Grid is issued for running various tests on different browsers, operating systems and machines in parallel. It used a hub and node concept. You don’t want test cases to run on a single machine. You have your machine as a hub and various systems on which test cases will be distributed. You call those machines as nodes.

The hub is the central point and there should be only one hub in a system. This hub is your machine from which you want to distribute the test cases among all the clients. This is the machine on which test cases will run and those will be executed on the nodes. There can be more than one node. Nodes can have different configurations with different platforms and browsers.

Let’s see how you can establish a selenium grid in your machine.

You can download the selenium grid server from the official site. You can place Selenium server.jar in a hard drive. You have to place it in all nodes and hub c drive. Open the command prompt in your hub and then, go to C directory. There you have to fire the below command java -jar selenium-server-standalone-2.30.0.jar -role hub

Now, if you want to check if the selenium grid server is running on localhost that is 4040 port or not. You can visit the local host on your browser.

You have to then go to C directory of the node and open command prompt in it. Also, when you have made the selenium grid server up in the hub, you have to note down the IP address and port.

java -Dwebdriver.gecko.driver=”C:\geckodriver.exe” -jar selenium-server-standalone-3.4.0.jar -role webdriver -hub ip address of hub -port port number of hub

When you fire the above command, again go to hub browser and the port number 4040. Refresh the page and you will see IP address which will be linked to the hub.

Now, you have set up machines with different configurations. But how would the hub know which test case would be run on which node.

This will be done by desired capabilities which will tell that this test script would run on a particular set of configuration.

One can do it using below source code:

This way, you will be able to distribute the test cases across different machines and browsers. You will be able to do parallel testing using Selenium Grid.

Know More: Selenium Tutorials For Beginners: An overall View into this Blog

Maven and Jenkins Integration with Selenium

Maven is a build management tool which is used to make the build process easy. Maven avoids hard coding of jars and you can easily share the project structure across the team using artifact and version id.

Every team member would be at the same page. You can make a maven project by going to File – New – Other – Maven project. You have to then specify the artifact id and then version id. You will be prompted to select a template. For starting, select a quik start template.

You will get a folder structure where you will be having two folders – src/main/java and src/main/test. In java folder, you will maintain all other stuff except tests while in test folder you will maintain all the test cases. You will be having pom.xml file where you have to define all the dependencies so that it can download from the maven repository and place them in ./m2 repository in your local project structure. You can get the dependency from the maven official site and then place in pom.xml. It will download all the required jars.

Also, Jenkins issued for continuous integration of the latest build with the production environment. Whenever a developer will fire the latest build then the smoke test will start running on that build. If the build gets passed then it can be deployed to the production else the developer and tester would get a notification about the failed build. It makes the delivery cycle very fast.

Also, you can do it by downloading Jenkins war file and then running it so that Jenkins server will be up and running on port number 4040.

After doing that you can install all the necessary plug-ins. You can then create a new item and does the post-build actions.

Also, you can pass the path of git repository from which it will fetch the latest build. If you are using a local server then you pass the path of pom.xml from the system.

You can even set nightly runs with Jenkins, You have to specify the time when you want your test to run. They will run and you will get the reports on the Jenkins server the next morning. Isn’t it time-saving?

Database Testing using Selenium WebDriver

Integrating Selenium tests to the database is so easy in the latest version of the tool. You have to make use of JDBC library for database connections. It allows connection of Java and databases. First, make a connection to the database with the following command:

DriverManager.getConnection(URL, “userid”, “password” )

After doing that you can load the JDBC driver. You can do it by following lines of code:

Class.forName(“com.mysql.jdbc.Driver”);

Now, you have to send the query to the database. How will you do it? You can create an object of a statement class and then execute the query using the statement object.

Statement stmt = con.createStatement();

stmt.executeQuery(select *  from employee;);

Now, you can get the result in the result set. You can do it by following command:

ResultSet rs= stmt.executeQuery(query);

After doing all the operations, you can close the connection.

Con.close();

In this way, you can get the result set and then do all the validations on the result set.

How to take screenshots in selenium web driver

You should get the screenshot of the failed test cases so that you can get to know where exactly is the problem. This will help in better-debugging skills.

Let’s look at the steps involved in taking a screenshot:

TakesScreenshot scrShot =((TakesScreenshot)webdriver);

This step caste a driver instances to Takes Screenshot interface. Now, you have one method which is getting screenshots which will get the image file.

File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

Now, you have taken the screenshot but you have to place it somewhere. You can create some folder which will be used for storing all the screenshots captured during the execution.

You can do it by following the line of code:

FileUtils.copyFile(SrcFile, DestFile);

This is the way to capture screenshots. But if you want that only the screenshot is taken when a test case fails. You can use ITestListener. It has various methods and one of the methods is onFailure() which will do the specified task when every there is any failure in a test case. So you can put this code in that method so that whenever any test fails it will take the screenshot and place it in the folder specified by you.

Know More: Selenium Intermediate Level Tutorials – To Learn Better

How to drag and drop in a web page using Selenium Web driver

Now if you want o drag and drop something o a web page, you would be confused. It is very simple. You have to make use of Actions class which gives you a method dragAndDrop() which will drag and drop a web element from a source destination to the desired destination.

Actions actions = new Actions(driver);

Actions. dragAndDrop(Sourcelocator, Destinationlocator).build().perform();

Make sure that the source locator and destination locator have correct xpaths. In this way, you will be able to drag and drop any element on a web page.

How to do right click and double click on a web page

You can do a right click on a web page using Actions class. Actions class provides doubleClick method which allows to double click on a web element. You can do it by following lines of code:

Actions actions = new Actions(driver);

WebElement elementLocator = driver.findElement(By.id(“ID”));

actions.doubleClick(elementLocator).perform();

You have to do right click in selenium using action class only. It is very easy. It provides a method – contextClick() and then right click on a web element.

Actions actions = new Actions(driver);

WebElement elementLocator = driver.findElement(By.id(“ID”));

actions.contextClick(elementLocator).perform();

This way, you will be able to right click and double click on a web element.

How to switch to an iFrame

iFrame is a web page which is embedded into another web page. Whenever you want to click on a web element, which is in another iframe. First, you have to switch to that iframe and then perform an action on it.

You can do switching by

Driver.switchTo().frame(index or name or id of iframe);

Conclusion

Now, you are all ready to rock the interviews with our advanced course. Prepare well and give your best. All the best!!

Mail

Hire a Tester

WhatsApp

Call Us

Testbytes