Skip to content

Instantly share code, notes, and snippets.

@RDayal11
Last active September 24, 2021 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RDayal11/1440f06481e4fda2290da2c30d836b91 to your computer and use it in GitHub Desktop.
Save RDayal11/1440f06481e4fda2290da2c30d836b91 to your computer and use it in GitHub Desktop.
package util;
import org.testng.*;
public class Listener implements ITestListener {
// This belongs to ITestListener and will execute before the whole Test starts
@Override
public void onStart(ITestContext arg0) {
Reporter.log("About to begin executing Class " + arg0.getName(), true);
}
// This belongs to ITestListener and will execute, once the whole Test is finished
@Override
public void onFinish(ITestContext arg0) {
Reporter.log("About to end executing Class " + arg0.getName(), true);
}
// This belongs to ITestListener and will execute before each test method
@Override
public void onTestStart(ITestResult arg0) {
Reporter.log("Testcase " + arg0.getName() + " started successfully", true);
}
// This belongs to ITestListener and will execute only on the event of successfull test method
public void onTestSuccess(ITestResult arg0) {
Reporter.log("Testcase " + arg0.getName() + " passed successfully", true);
}
// This belongs to ITestListener and will execute only on the event of fail test
public void onTestFailure(ITestResult arg0) {
Reporter.log("Testcase " + arg0.getName() + " failed", true);
}
// This belongs to ITestListener and will execute only on the event of skipped test method
public void onTestSkipped(ITestResult arg0) {
Reporter.log("Testcase " + arg0.getName() + " got skipped", true);
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>LambdaTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
package LamdaTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
@Listeners({util.Listener.class})
class ReporterTest {
public String username = "riadayal";
public String accesskey = "BKnKA7UZPf0R7VdtmJSsq6AEA86R8wBNVch1MbLvtBzosqbNNA";
public static RemoteWebDriver driver = null;
public String gridURL = "@hub.lambdatest.com/wd/hub";
@BeforeTest
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "chrome");
capabilities.setCapability("version", "93.0");
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
capabilities.setCapability("build", "TestNGReporterLog");
capabilities.setCapability("name", "TestNGReporterLogSample");
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());
}
}
@Test
public void learnSingleInputFieldUsingSelenium() {
try {
Reporter.log("Logging into Selenium Playground");
driver.get("http://labs.lambdatest.com/selenium-playground/");
Reporter.log("Logged into Selenium Playground. Now, Clicking On Simple Form Demo", true);
//Clicks on the simple form demo option in the selenium playground
WebElement simpleFormDemo = driver.findElement(By.xpath("//a[text()='Simple Form Demo']"));
simpleFormDemo.click();
Reporter.log("Clicked successfully on Simple Form Demo option.", 2);
//Enters the message in the enter message input box
WebElement messageInputBox = driver.findElement(By.xpath("//input[@id='user-message']"));
messageInputBox.sendKeys("Running my first testcase using Lambda Test");
//Clicks on Show Message button
WebElement showMessageButton = driver.findElement(By.xpath("//button[text()='Show Message']"));
showMessageButton.click();
//Retrieves the entered user message
WebElement userMessage = driver.findElement(By.xpath("//label[text()='Your Message: ']//parent::div//span"));
String actualUserText = userMessage.getText();
Reporter.log("Actual User Input Is: " + actualUserText, 2, true);
Assert.assertTrue(actualUserText.equals("Running my first testcase using Lambda Test"), "Expected and actual texts do not match.");
} catch (Exception e) {
}
}
@Test
public void learnMultipleInputFieldUsingSelenium() {
try {
Reporter.log("Logging into Selenium Playground");
driver.get("http://labs.lambdatest.com/selenium-playground/");
Reporter.log("Logged into Selenium Playground. Now, Clicking On Simple Form Demo", true);
WebElement simpleFormDemo = driver.findElement(By.xpath("//a[text()='Simple Form Demo']"));
simpleFormDemo.click();
Reporter.log("Clicked successfully on Simple Form Demo option For Multiple input fields.", 2);
WebElement firstInputBox = driver.findElement(By.xpath("//input[@id='sum1']"));
firstInputBox.sendKeys("2");
WebElement secondInputBox = driver.findElement(By.xpath("//input[@id='sum2']"));
secondInputBox.sendKeys("2");
WebElement getTotalButton = driver.findElement(By.xpath("//button[text()='Get Total']"));
getTotalButton.click();
WebElement userMessage = driver.findElement(By.xpath("//label[text()='Total a + b = ']//parent::div//span"));
String actualUserText = userMessage.getText();
Reporter.log("The total of the two entered user inputs is: " + actualUserText, 2, true);
Assert.assertTrue(actualUserText.equals("4"), "Expected and actual texts do not match.");
} catch (Exception e) {
}
}
@AfterTest
public void closeBrowser() {
driver.close();
Reporter.log("The driver has been closed.", false);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="TestNGReporterSuite" parallel="classes" verbose="10">
<test name="TestNGReporterTest" preserve-order="true">
<classes>
<class name="LamdaTest.ReporterTest">
<methods>
<include name="learnMultipleInputFieldUsingSelenium"/>
<include name="learnSingleInputFieldUsingSelenium"/>
</methods>
</class>
</classes>
</test>
</suite>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment