In this tutorial, you will learn,
create a beautiful report for test cases
I'm going to work on the same project we are previously scripted you can visit that article from click on this.
we can display our test data as well as the behaviour of our script.
first of all, we need to add extent reports in our pom.xml
<!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
if you not familiar with that use this link after that, we can call extent report methods like given example we need to create an HTML document and need to give a path now we can start a test. as given in the example.
static ExtentTest test;
static ExtentReports report;
report = new ExtentReports(System.getProperty("user.dir")+"\\Reports\\ExtentReport.html");
test = report.startTest("Start of Test Case");
startTest()
using "startTest()" we can provide test cast case name. Calling startTest() generates a toggle for the test in the HTML file and adds all log events under this level. This is a required step and without calling this method the toggle will not be created for the test and log will not be added.
log()
this is basically a logger using "log()" we can provide. Log event is shown in the report with 3 columns
as Timestamp, Status, Details and StepName.
under this, we can provide a logger status as
LogStatus.PASS
LogStatus.FAIL
LogStatus.INFO
FATAL
SKIP
UNKNOWN
WARNING
finally need to end test usingendTest(test), and flush(); If flush()
is called before any of the ended tests, no information will be appended.
using endTest test case ends and prepares the test to be added to the report on flush()
report.endTest(test);
report.flush();
full code
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import org.testng.annotations.BeforeMethod;
import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TechbitesLogin {
static WebDriver ChromeDriver;
static String ChromeDriverLocation = System.getProperty("user.dir")+"\\Driver"+"\\chromedriver.exe";
static ExtentTest test;
static ExtentReports report;
@BeforeTest
public static void OpenBrowser(){
report = new ExtentReports(System.getProperty("user.dir")+"\\Reports\\ExtentReport.html");
test = report.startTest("Start of Test Case");
System.setProperty("webdriver.chrome.driver", ChromeDriverLocation);
ChromeDriver = new ChromeDriver();
test.log(LogStatus.PASS, "Driver initiate sucessfully");
ChromeDriver.manage().window().maximize();
test.log(LogStatus.PASS, "Browser maximized");
ChromeDriver.get("https://opensource-demo.orangehrmlive.com/");
test.log(LogStatus.PASS, "https://opensource-demo.orangehrmlive.com/");
test.log(LogStatus.PASS, "Driver initiate sucessfully");
}
@Test(priority = 1)
public void Login(){
String userName= "Admin";
String password= "admin123";
// web element for user name
WebElement element = ChromeDriver.findElement(By.xpath("//input[@name='txtUsername']"));
//clear text field value
element.clear();
//type "Admin" in web element
test.log(LogStatus.INFO,"clear");
element.sendKeys(userName);
test.log(LogStatus.PASS,"type user name",userName);
//web element for password
ChromeDriver.findElement(By.xpath("//input[@id='txtPassword']")).sendKeys(password);
test.log(LogStatus.PASS,"type password",password);
//click on button
WebElement element2 = ChromeDriver.findElement(By.xpath("//input[@value='LOGIN']"));
element2.click();
test.log(LogStatus.WARNING, "click on button", element2.toString());
System.out.println("End of login");
}
@Test(priority = 2)
public static void Getobject() {
//wait time unites (60)
ChromeDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//set locator type
By mySelector = By.xpath("//div[@id='mainMenu']/ul/li/descendant::b");
test.log(LogStatus.INFO, "click on", mySelector.toString());
//create list
List<WebElement> myElements = ChromeDriver.findElements(mySelector);
// loop through element count
for (int a = 1; a <= myElements.size(); a++) {
//iniger convert to String
String number = Integer.toString(a);
test.log(LogStatus.INFO, "number of items =", number);
System.out.println(number.toString());
//pause time
ChromeDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
//web element for read value of attribute
WebElement name = ChromeDriver
.findElement(By.xpath("(//div[@id='mainMenu']/ul/li/descendant::b)[" + a + "]"));
test.log(LogStatus.PASS, "Xpath =", name.toString());
//extract text in web element
String output = name.getText();
System.out.println(output);
test.log(LogStatus.PASS, "item name", output);
}
}
@AfterTest
public void Close()
{
ChromeDriver.quit();
report.endTest(test);
report.flush();
}
}
No comments:
Post a Comment