Popup Handling in Selenium

 

There are different types of popup and alerts in Web application some are javascript popups and some are HTML for handling them in selenium WebDriver there are diff ways.

What is an Alert?

Alert is a small message box which displays the on-screen notification to give the user some kind of information or ask for permission to perform certain kind of operation. It may be also used for warning purpose.

Different types of Alerts

Simple Alert
Simple alerts just have an OK button on them. They are mainly used to display some information to the user. The first alert on our test page is a simple alert. The following code will read the text from the Alert and then accept the alert. An important point to note is that we can switch from the main window to an alert using the driver.SwitchTo().Alert().

Prompt Alerts
In prompt alerts, you get an option to add text to the alert box. This is specifically used when some input is required from the user. We will use the SendKeys() method to type something in the Prompt alert box.

Confirmation Alert
This alert comes with an option to accept or dismiss the alert. To accept the alert you can use IAlert.Accept() and to dismiss you can use the IAlert.Dismiss().

JavaScript Popup: – we can’t inspect javascript popup because it is not written in HTML also we can’t move javascript popups. In order to handle that popup, there is an interface called Alert.

In order to handle javascript popup first, we have to switch the driver control to the Javascript popup.For switch the Control we have to use driver.switchTo().alert() .
Methods present in Alert interface
accept();
dismiss();
sendkeys();
getText();

 

 

File Uploading Popup: – For uploading file uploading popup we can use sendKeys(); or we can use window automation tools like AutoIt and Sikuli. Also, we can’t inspect and move these popups.

Using the sendKeys Method

 

Using Sikuli with selenium:- sikuli is an open source image recognition automation tool which is used to automate the desktop applications, web applications, and gaming applications. To use sikuli with selenium copy sikulixapi.jar and in the libraries.

 

Authentication Popup:- The title of this popup page is authentication required and this popup also contains 2 fields Username and Password.

 

File Download Popup Handel using AutoIt:- AutoIt is an open source window automation tool which uses a basic scripting language for writing that script we use “AutoIt Script Editor” and for inspecting the popup we use “AutoIt window info tool”.
1. First, we have to inspect the popup using “Finder Tool”.

2. Open SciTE Script and AutoIt code and save that AutoIt code with the extension of “.au3” then right click on that file and compile Script which will generate a “.exe” file.

 

Conclusion
In this tutorial, we tried to make you acquainted with the WebDriver’s Alert class that is used to handle web-based pop-ups. We discussed the different types of popup and alerts in a Web application. Some are javascript popups and some are HTML for handling them in selenium WebDriver there are diff ways.

How to run your test on multiple machines using Selenium Grid

 

About Selenium Grid

Selenium-Grid permits you run your tests on various machines against various programs in parallel. That is, running numerous tests in the meantime against various machines running diverse browsers and working frameworks. Basically, Selenium-Grid bolsters circulated test execution. It takes into account running your tests in a distributed test execution condition.  

Working of Selenium-Grid with a Hub and Nodes

A grid comprises of a solitary hub, and at least one node. Both will utilize the selenium-server.jar executable.

The hub gets a test to be executed alongside data on which browser and ‘platform’ (i.e. WINDOWS, LINUX, and so forth) where the test ought to be run. It ‘knows’ the setup of every node that has been ‘enlisted’ to the hub. Utilizing this data, it chooses an accessible node that has requested for the browser-platform combination.  Once a node has been chosen, Selenium commands initiated by the test are sent to the hub, which passes them to the hub relegated to that test. The node runs the browser and executes the Selenium orders inside that browser against the application under test.

Process

In order to run all the test scripts, present in the framework on a remote computer, we use selenium grid. In selenium grid, there is two type of systems first is HUB and other is a NODE.

HUB: – It is a server computer which controls the execution of the selenium grid.

NODE: – It is the remote Computer which accepts the script from the HUB and executes the test scripts, Multiple nodes systems can communicate with a single hub system.

  For using selenium grid, we have to perform a few steps.

  • Start the HUB
  • Start the NODE

Execute our Framework using remote WebDriver

1. Start the HUB

In the server computer copy selenium-server jar file paste it inside the (c: drive) or any location you want to.
Open the command prompt on the exact location where you have that selenium-server jar file and type the following command java -jar c:/selenium-server-standalone-2.53.1.jar -role hub. 

It should show the following message-
Nodes should register to http://192.168.0.167:4444/grid/register/
Selenium Grid hub is up and running

  The default port number for the HUB is 4444.

2. Start the NODE 

On the remote computer where you want to run the test script copy selenium-server jar file paste it inside the (c: drive) or any location, you want to.
Open the command prompt on the exact location where you have that selenium-server jar file and type the following command java -jar c:/selenium-server-standalone-2.53.1.jar -role node –hub http://192.168.0.167:4444/
Note- copy the hub URL from your HUB system.
And it should show the following message-
Registering the node to the hub: http://192.168.0.167:4444/grid/register
The node is registered to the hub and ready to use

And on your HUB system, you will also get a message-
Registered a node http://192.168.0.167:5555

The default port no for a node is 5555.

3. Execute our Framework using remote WebDriver

To execute the test scripts in a remote computer we have to use the remote web driver class in order to specify the browser which should be open on a remote computer we should use desired capability class.

For executing test scripts in NODE system simply execute test script from the HUB system.

Conclusion

Selenium Grid reduces a lot of batch execution time which can be counted as an advantage.  It can also perform cross-browser testing and perform multiple OS testing. The basic concept of Selenium Grid is that it allows you to run your test on multiple machines and on multiple browsers.

Testing Next Generation – TestNG

 

TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionality that makes it more powerful and easier to use.

It is an open-source automated testing framework where NG of TestNG means Next Generation.  

Why Test NG

These are the major advantages of Test NG

  • Test NG has advanced features which help us to easily maintain our automation scripts.
  • It can achieve Batch execution and Parallel execution
  • It can achieve Group execution
  • It gives the ability to produce HTML Reports of execution  
  • Data Parameterization is possible  

Annotations available in TestNG

  • @BeforeSuit
  • @AfterSuit
  • @BeforeTest
  • @AfterTest  
  • @BeforeClass  
  • @AfterClass
  • @BeforeMethod 
  • @AfterMethod
  • @Test
  • @Parameters
  • @DataProvider

To use TestNG first you have to install Test NG plugin in Eclipse IDE.

Test NG Class: The class which contains at least one @Test annotation is called TestNG class.

The output is:-

This is Before Method
My First Test
This is After Method
This is Before Method
My Second Test
This is After Method
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Batch Execution & Run Script Multiple times  

Batch Execution: Executing multiple scripts or multiple TestNG class is called Batch Execution. For achieving batch execution:-  

  1. We have to generate a TestNG suit file.
  2. Add all the classes in the TestNG classes as shown below.   

For generating TestNG suit simply just Right click on TestNG class and move to TestNG and click on Convert to TestNG. We can also create a suit file for a package which contains all TestNG classes by just right-clicking on the package and following the same.

Run Script Multiple Times: To run a test script multiple times we have to use InvocationCount after @Test annotation.

The output is:-  

This is Before Method
My First Test
This is After Method
This is Before Method
My First Test
This is After Method
This is Before Method
My First Test
This is After Method
This is Before Method
My First Test
This is After Method
This is Before Method
My First Test
This is After Method
This is Before Method
My Second Test
This is After Method
===============================================
Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================

For changing the order of script execution:

There are different ways to change the order of script execution for TestNG script.

  1. By using ‘priority’ attribute: If we want to change the order of executing script we can use ‘(priority=1)’ after @Test. TestNG by default will execute the scripts and class in Alphabetic order.

The output is:-  

My Third  Test
My Second Test
My First Test
===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

  1. By Using ‘dependsOnMethods’ attribute: For changing the order of executing script we can also use ‘(dependsOnMethods = { “methodname” })’ after @Test.

The output is:-

My Third  Test
My Second Test
My First Test
===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 0
=================================================

  1. By using TestNG Suit file: For changing the order of executing script we can also use includeTag (<include name=“methodname”></include>) in suit file.

The output is:-

My Third  Test
My Second Test
My First Test
===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 0
===========================================

Parallel Execution:-

We use parallel script execution to test the Script with multiple browsers in order to check compatibility. This is called cross-browser testing. To achieve parallel Script execution, we use parameter tag in test runner in the TestNG Suit file and use one of the annotation @Parameters in TestNG Class.

As a developer, you will find TestNG very useful as it gives the developer the ability to write more flexible and powerful tests with help of easy annotations, grouping, sequencing & parametrizing.

How to integrate Maven Project from Bitbucket to Continuous Integration Jenkins

First, let’s understand what Continuous integration is. It’s a process which allows you to identify errors in the starting. All development work is integrated as early as possible and artifacts are created and tested automatically.

Now, what is Bitbucket?

Bitbucket is the most commonly used tool for version control system, the whole team can add and commit the test scripts that are developed locally and push the changes to the bit.

How is Jenkins used?

Jenkins is a widely used open source tool to perform continuous integration and build automation. It monitors the execution of the steps and allows to stop the process if one of the steps fails.

Jenkins can pull the source code from Bitbucket server by selecting the option of source code management and specify the SSH key and can find the source code of your project.

Now we will take you through the process step by step.

  1. To setup Jenkins, first of all, you have to download Jenkins.war file from the link- https://jenkins.io and copy that file to your c drive.
  2. Open a command prompt and execute —
    1. java –jar c:/ jenkins.war
  3. When the installation is complete the browser will be launched. If it doesn’t, open a browser- To browse http://localhost:8080
  4. To unlock Jenkins page, paste this password into the Administrator password field and click Continue.
  5. When Jenkins is up and running you can click on Manage Jenkins.
  6. After that click on Manage Plugins.
  7. Move to Available tab
  8. Search for Maven Integration plugin and Bit Integration by clicking on the checkbox.
    Maven Integration plugin will allow you to add a maven project and Bit Integration plugin will allow you to pull source code from the repository.
  9. Then click install without start and wait for the installation, then click on restart.
  10. Comeback to Manage Jenkins page.
  11. Click on Global Tool Configuration.
  12. Click on JDK Installations.
  13. Now add the JDK path.
  14. Click on Add Git and add the Git path.
  15. Click on Maven installations and add the Maven path.
  16. Click on save. The configuration is done. Now you just need to create a Maven project.
  17. To create a new Project, you have to Move to Jenkins dashboard and click on Create New Job.
  18. Just provide any Name for Project and select Maven Project for creating a maven project and then click on Ok.
  19. Select Git in Source Code Management and enter your repository clone SSH/HTTPS URL and add a valid username and password in credentials.
  20. Scroll Down to Build under this in Goal and Options. Enter clean install or as you need.
  21. Click on Save.
  22. Now you can click on Build and whenever you click on build, Jenkins will pull the latest commit source code from Bitbucket and generating a build. Also, it will generate a test report of that execution.Hope you got some insight on how to  integrate Maven Project from Bitbucket to Continues integration Jenkins and found our tips useful. Suggestions are always welcome and appreciated.