Monday, 29 April 2019

TestWork : Paraller Testing with TestNG and ThreadLocal in Selenium Java

//Base Class

package com.testcases;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BaseClass {
private WebDriver driver;
public Date d = new Date();
public SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public String BROWSER = "chrome";
static ThreadLocal<WebDriver> dr = new ThreadLocal<WebDriver>();

public void setdriver(WebDriver driver) {
dr.set(driver);
}

public WebDriver getdriver() {
return dr.get();
}

public void init() {
if (BROWSER.contains("chrome")) {
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver (2).exe");
driver = new ChromeDriver();
setdriver(driver);
getdriver().manage().deleteAllCookies();
getdriver().manage().window().maximize();

}

}

public void quit() {
getdriver().quit();

}

}



//Test Class 1

package com.testcases;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ClassOne extends BaseClass {
Date d = new Date();

@BeforeMethod
public void start() {
init();
// df.format(date)

}

@AfterMethod
public void end() {
quit();
}

@Test
public void methodone() throws Throwable {
System.out.println("method 1 " + df.format(d) + "Thread " + Thread.currentThread().getName());
getdriver().get("http://www.google.com");
getdriver().findElement(By.name("q")).sendKeys("method 1");
}

@Test
public void methodtwo() throws Throwable {
System.out.println("method 2 " + df.format(d) + "Thread " + Thread.currentThread().getName());
getdriver().get("http://www.google.com");
getdriver().findElement(By.name("q")).sendKeys("method 1");
Thread.sleep(3000);
}

}


//Class two

package com.testcases;

import java.util.Date;

import org.openqa.selenium.By;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Classtwo extends BaseClass {

@BeforeMethod
public void start() {
init();
}

@AfterMethod
public void end() {
quit();
}

@Test
public void methodthree() throws Throwable {
System.out.println("method 3 " + df.format(d) + "Thread " + Thread.currentThread().getName());
getdriver().get("http://www.rediff.com");
// getdriver().findElement(By.name("q")).sendKeys("method 3");
}

@Test
public void methodfour() throws Throwable {
System.out.println("method 4 " + df.format(d) + "Thread " + Thread.currentThread().getName());
getdriver().get("http://www.wwe.com");
// getdriver().findElement(By.name("q")).sendKeys("method 4");
Thread.sleep(3000);
}

}

//testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes">
  <test thread-count="5" name="Test" parallel="classes">
    <classes>
      <class name="com.testcases.Classtwo"/>
      <class name="com.testcases.ClassOne"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

No comments:

Post a Comment

Spring Boot : Exception Handler 14