Skip to content

Instantly share code, notes, and snippets.

@muditlambda
Created December 29, 2021 11:46
Show Gist options
  • Save muditlambda/02d8ea1293ab84ca5124135ec1c7a57a to your computer and use it in GitHub Desktop.
Save muditlambda/02d8ea1293ab84ca5124135ec1c7a57a to your computer and use it in GitHub Desktop.
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class OrderTests {
String urlToTest = "https://www.lambdatest.com/selenium-playground/simple-form-demo.php";
WebDriver driver;
@BeforeAll
public static void start() {
System.out.println("=======Starting junit 5 tests========");
}
@BeforeEach
public void setup() {
System.out.println("Setting up the drivers");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shalini\\Downloads\\chromedriver_latest94\\chromedriver.exe");
driver = new ChromeDriver();
}
@Test
@Order(2)
public void enterAndDisplayMessage() {
String inputString ="Hello World";
String methodName = Thread.currentThread()
.getStackTrace()[1]
.getMethodName();
System.out.println("********Execution of "+methodName+" has been started********");
System.out.println("Launching LambdaTest selenium playground website started..");
driver.get(urlToTest);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
WebElement textBox = driver.findElement(By.xpath("//input[@id='user-message']"));
textBox.sendKeys(inputString);
WebElement showButton = driver.findElement(By.id("showInput"));
showButton.click();
WebElement messageDisplayed = driver.findElement(By.xpath("//div[@id='user-message']//span[@id = 'message']"));
String message = messageDisplayed.getText();
Assertions.assertEquals(inputString,message);
System.out.println("********Execution of "+methodName+" has ended********");
}
@Test
@Order(1)
public void addAndDisplayResult(){
String methodName = Thread.currentThread()
.getStackTrace()[1]
.getMethodName();
System.out.println("********Execution of "+methodName+" has been started********");
System.out.println("Launching LambdaTest selenium playground website started..");
driver.get(urlToTest);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
WebElement input1 = driver.findElement(By.id("sum1"));
input1.sendKeys("1");
WebElement input2 = driver.findElement(By.id("sum2"));
input2.sendKeys("3");
WebElement addButton = driver.findElement(By.xpath("//*[@id='gettotal']//button[contains(@class,'selenium_btn')]"));
addButton.click();
WebElement resultDisplayed = driver.findElement(By.id("addmessage"));
String result = resultDisplayed.getText();
Assertions.assertEquals("4",result);
System.out.println("********Execution of "+methodName+" has ended********");
}
@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