Skip to content

Instantly share code, notes, and snippets.

@muditlambda
Created December 29, 2021 11:43
Show Gist options
  • Save muditlambda/309aa14747a4bdce4027b4812e6bf02b to your computer and use it in GitHub Desktop.
Save muditlambda/309aa14747a4bdce4027b4812e6bf02b to your computer and use it in GitHub Desktop.
package ParallelTestsInJnit;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static org.junit.jupiter.params.provider.Arguments.arguments;
public class ParallelTestsInGrid {
String username = "YOUR_USERNAME"; //Enter your username
String accesskey = "YOUR_ACCESSKEY"; //Enter your accesskey
static RemoteWebDriver driver = null;
String gridURL = "@hub.lambdatest.com/wd/hub";
String urlToTest = "http://labs.lambdatest.com/selenium-playground/";
@BeforeAll
public static void start() {
System.out.println("=======Running junit 5 tests in parallel in LambdaTest Grid has started========");
}
public void setup(String browser) {
System.out.println("Setting up the drivers and browsers");
DesiredCapabilities capabilities = new DesiredCapabilities();
if(browser.equals("Chrome")) {
capabilities.setCapability("platform", "Windows 10");// To specify the OS
capabilities.setCapability("browserName", "Chrome"); //To specify the browser
capabilities.setCapability("version","94.0"); //To specify the browser
capabilities.setCapability("build", "ChromeTests"); //To identify the test
capabilities.setCapability("name", "Parallel_JUnit5Tests");
}
else if (browser.equals("Firefox")){
capabilities.setCapability("browserName", "Firefox"); //To specify the browser
capabilities.setCapability("version", "93.0"); //To specify the browser version
capabilities.setCapability("platform", "Windows 10"); // To specify the OS
capabilities.setCapability("build", "FirefoxTests"); //To identify the test
capabilities.setCapability("name", "Parallel_JUnit5Tests");
}
else if (browser.equals("Edge")){
capabilities.setCapability("browserName", "MicrosoftEdge");
capabilities.setCapability("platform", "Windows 10");
capabilities.setCapability("version","94.0"); // To specify the OS
capabilities.setCapability("build", "EdgeTests"); //To identify the test
capabilities.setCapability("name", "Parallel_JUnit5Tests");
}
capabilities.setCapability("network", true); // To enable network logs
capabilities.setCapability("visual", true); // To enable step by step screenshot
capabilities.setCapability("video", true); // To enable video recording
capabilities.setCapability("console", true); // To capture console logs
try {
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@ParameterizedTest
@MethodSource("browser")
@DisplayName("LoginTest")
@Order(1)
public void test1(String browser){
setup(browser);
String methodName = Thread.currentThread()
.getStackTrace()[1]
.getMethodName();
System.out.println("********Execution of "+methodName+" has been started********");
driver.get(urlToTest);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
List<WebElement> elements = driver.findElements(By.xpath("//a[@class='nav-link']"));
for(WebElement e : elements){
if (e.getText().equals("Login")){
e.click();
WebElement username = driver.findElement(By.id("email"));
username.sendKeys("testuser");
WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
password.sendKeys("mypassword");
WebElement loginBtn = driver.findElement(By.xpath("//button[@id='login-button']"));
loginBtn.click();
}
}
System.out.println("********Execution of "+methodName+" has ended********");
}
@ParameterizedTest
@MethodSource("browser")
@DisplayName("FormTest")
@Order(2)
public void test2(String browser){
setup(browser);
String methodName = Thread.currentThread()
.getStackTrace()[1]
.getMethodName();
System.out.println("********Execution of "+methodName+" has been started********");
driver.get(urlToTest);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[contains(@class,'btn_close_ck')]")).click();
WebElement element = driver.findElement(By.linkText("Simple Form Demo"));
element.click();
driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);
WebElement textBox = driver.findElement(By.xpath("//input[@id='user-message']"));
textBox.sendKeys("Hello World");
WebElement submitBtn = driver.findElement(By.id("showInput"));
submitBtn.click();
System.out.println("********Execution of "+methodName+" has ended********");
}
@ParameterizedTest
@MethodSource("browser")
@DisplayName("CheckBoxTest")
@Order(3)
public void test3(String browser){
setup(browser);
String methodName = Thread.currentThread()
.getStackTrace()[1]
.getMethodName();
System.out.println("********Execution of "+methodName+" has been started********");
driver.get(urlToTest);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.xpath("//a[contains(@class,'btn_close_ck')]")).click();
WebElement option = driver.findElement(By.linkText("Checkbox Demo"));
option.click();
driver.manage().timeouts().pageLoadTimeout(10,TimeUnit.SECONDS);
WebElement checkBox1 = driver.findElement(By.xpath("//input[@id='ex1-check1']"));
checkBox1.click();
boolean flag = checkBox1.isSelected();
if(!flag){
Assertions.fail("The checkbox is not selected");
}
System.out.println("********Execution of "+methodName+" has ended********");
}
static Stream<Arguments> browser() {
return Stream.of(
arguments("Chrome"),
arguments("Firefox"),
arguments("Edge")
);
}
@AfterEach
public void tearDown() {
System.out.println("Quitting the browsers has started");
driver.quit();
System.out.println("Quitting the browsers has ended");
}
@AfterAll
public static void end() {
System.out.println("Tests ended");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment