Selenium
Locate Elements by Link Text & Partial Link Text in Selenium Webdriver
What is Link Text in Selenium? A Link Text in Selenium is used to identify the hyperlinks on a web...
To understand how to run scripts in parallel, let's first understand
During test execution, the Selenium WebDriver has to interact with the browser all the time to execute given commands. At the time of execution, it is also possible that, before current execution completes, someone else starts execution of another script, in the same machine and in the same type of browser.
In such situation, we need a mechanism by which our two different executions should not overlap with each other. This can be achieved using Session Handling in Selenium.
If you check the source code of Selenium WebDriver, you will find a variable named as 'sessionId'. Whenever we create a new instance of a WebDriver object, a new 'sessionId' will be generated and attached with that particular Firefox/Chrome/IE Driver ().
So anything we do after this will execute only in that particular Firefox browser session.
As this is an in-built functionality, there is no explicit need to assign the session id
Code Example: Here two different sessions will be generated for two different WebDriver.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SessionHandling {
public static void main(String...strings ){
//First session of WebDriver
WebDriver driver = new FirefoxDriver();
//Goto gtupapers site
driver.get("http://demo.gtupapers.com/V4/");
//Second session of WebDriver
WebDriver driver2 = new FirefoxDriver();
//Goto gtupapers site
driver2.get("http://demo.gtupapers.com/V4/");
}
}There are situations where you want to run multiple tests at the same time.
In such cases, one can use "parallel" attribute
The parallel attribute of suite tag can accept four values:
| tests | All the test cases inside <test> tag of Testing xml file will run parallel. |
| classes | All the test cases inside a Java class will run parallel |
| methods | All the methods with @Test annotation will execute parallel. |
| instances | Test cases in same instance will execute parallel but two methods of two different instances will run in different thread. |
The attribute thread-count allows you to specify how many threads should be allocated for this execution.
Complete Example: In this Example, three test cases will run parallel and fill login data in http://demo.gtupapers.com
The Complete project will look like:
TestgtupapersMultipleSession.java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TestgtupapersMultipleSession {
@Test
public void executSessionOne(){
//First session of WebDriver
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Goto gtupapers site
driver.get("http://demo.gtupapers.com/V4/");
//find user name text box and fill it
driver.findElement(By.name("uid")).sendKeys("Driver 1");
}
@Test
public void executeSessionTwo(){
//Second session of WebDriver
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Goto gtupapers site
driver.get("http://demo.gtupapers.com/V4/");
//find user name text box and fill it
driver.findElement(By.name("uid")).sendKeys("Driver 2");
}
@Test
public void executSessionThree(){
//Third session of WebDriver
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Goto gtupapers site
driver.get("http://demo.gtupapers.com/V4/");
//find user name text box and fill it
driver.findElement(By.name("uid")).sendKeys("Driver 3");
}
}TestNG.XML
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="3" parallel="methods" > <test name="testGuru"> <classes> <class name="TestgtupapersMultipleSession"> </class> </classes> </test> </suite>
You can set order and dependency of Test Case execution.
Suppose you have two test cases , 'testgtupapersTC1' and 'testgtupapersTC2' and you want to execute test case 'testgtupapersTC2' before 'testgtupapersTC1'. In that case we will use 'dependsOnMethods' attribute to make dependency and order of execution.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="TestSuite" thread-count="3" parallel="methods" > <test name="testGuru"> <classes> <class name="TestgtupapersMultipleSession"> <include value="testgtupapersTC1" dependsOnMethods=" testgtupapersTC2"/> <include value="testgtupapersTC2"/> </class> </classes> </test> </suite>
What is Link Text in Selenium? A Link Text in Selenium is used to identify the hyperlinks on a web...
What are Broken Links? Broken links are links or URLs that are not reachable. They may be down or...
SoapUI is the most popular open source functional Testing tool for Api Testing . It provides...
Robot Class Robot Class in Selenium is used to enable automated testing for implementations of Java...
Using the Java class "myclass" that we created in the previous tutorial, let us try to create a...
Accessing Image Links Image links are the links in web pages represented by an image which when...