我正在使用扩展区报告为使用Page对象模型的硒测试生成日志,并且我具有用于页面和测试的单独类。我能够生成范围报告日志,但是我的登录页面日志正在重复。我怀疑这是因为在所有其他测试中都使用了登录名,但是由于我是Java新手,所以无法解决此问题。请帮忙。
范围报告类别
package util;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class Reports {
public static ExtentReports extent;
public static ExtentTest test;
public static ExtentReports getInstance() {
if(extent == null) {
GetExtent();
}
return extent;
}
public static ExtentReports GetExtent() {
/*Create ExtentReports object passing location and report name as argument. Notice a new Result Log folder will be created inside project and the report name will be TestReport.html*/
extent = new ExtentReports(System.getProperty("user.dir") + "/RESULT_LOG" + "/TestReport.html", true);
// Add details to our report
extent.addSystemInfo("Selenium Version", "3.0.1").addSystemInfo("Environment", "QA");
return extent;
}
// Start Test Case
public void startTest (String testCaseName, String description) {
// Create ExtentTest passing test case name and description
test = extent.startTest(testCaseName, description);
}
// Log Test status, Test name and Test details
public void logStatus (LogStatus testStatus, String testStepName, String testDetails){
// Log test status
test.log(testStatus, testStepName, testDetails);
}
// Capture screenshot and log into report
public void screenshotLog (LogStatus logStatus, String testStepName, String screenShotPath){
// Attach screenshots
test.log(logStatus, testStepName + test.addScreenCapture(screenShotPath));
}
// End Test Case
public void endTest() {
// End test
extent.endTest(test);
extent.flush();
}
public void endReport(){
extent.close();
}
}
登录页面类
package pages;
import base.TestBase;
import com.relevantcodes.extentreports.LogStatus;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import util.Reports;
public class LoginPage extends TestBase {
Reports reports;
//Page Factories
@FindBy(xpath="//input[@name='email']")
WebElement username;
@FindBy(xpath="//input[@name='pwd']")
WebElement password;
@FindBy(xpath="//fieldset[@id='submitButton']")
WebElement loginBtn;
@FindBy(xpath = "//a[@title='guest']")
WebElement guestTab;
@FindBy(xpath = "//a[@title='grid']")
WebElement gridViewTab;
//Initializing the page objects
public LoginPage() {
PageFactory.initElements(driver, this);
reports = new Reports();
}
//Actions
public String validateLoginPageTitle() {
String ActualTitle =driver.getTitle();
reports.logStatus(LogStatus.INFO, "Get the title", "Title is fetched" + " <span class='label success'> Success</span>");
return ActualTitle;
}
public FloorView login(String un, String pw) throws InterruptedException {
username.sendKeys(un);
// Extent Report Logging. Enter Log status for HTML report
reports.logStatus(LogStatus.INFO, "Enter user name", "User name entered" + " <span class='label success'> Success</span>");
password.sendKeys(pw);
// Extent Report Logging. Enter Log status for HTML report
reports.logStatus(LogStatus.INFO, "Enter password", "Password entered" + " <span class='label success'>Success</span>");
loginBtn.click();
Thread.sleep(7000);
// Extent Report Logging. Enter Log status for HTML report
reports.logStatus(LogStatus.INFO, "Click Login", "Login button clicked" + " <span class='label success'> Success</span>");
return new FloorView();
}
public GuestPage navigateToGuests(){
guestTab.click();
return new GuestPage();
}
public GridView navigateToGridView(){
gridViewTab.click();
return new GridView();
}
}
登录页面测试
package testcases;
import base.TestBase;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.LogStatus;
import org.testng.Assert;
import org.testng.annotations.*;
import pages.FloorView;
import pages.LoginPage;
import util.Reports;
public class LoginPageTest extends TestBase {
LoginPage loginPage;
FloorView floorView;
Reports reports;
static ExtentReports extent;
public LoginPageTest() {
super();
}
@BeforeSuite
public void setupReport(){
extent = Reports.getInstance();
}
@BeforeMethod
public void setUp() {
initialization();
loginPage = new LoginPage();
reports = new Reports();
}
@Test(priority=1)
public void LoginPageTitleTest(){
// Start Extent Report
reports.startTest("Login Page Title Test","Launch application and verify that the correct web page is opened");
String title = loginPage.validateLoginPageTitle();
try {
Assert.assertEquals(title, "Eat App Restaurant");
reports.logStatus(LogStatus.PASS, "Validate Page title", "Page title Validated" + " <span class='label success'> Success</span>");
} catch (AssertionError e) {
reports.logStatus(LogStatus.FAIL, "Validate Page title", "Wrong title, test failed" + " <span class='fail label'>Fail</span>");
}
}
@Test(priority=2)
public void LoginTest() throws InterruptedException {
// Start Extent Report
reports.startTest("Login Test","Navigate to Application, verify Login page is opened. Login to the application and verify that login is successful, and floor view is opened by default.");
floorView = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
String currentURL = driver.getCurrentUrl();
if(currentURL.equals("https://cactus-staging.netlify.com/floor")) {
reports.logStatus(LogStatus.PASS, "Verify login is successful", "Logged in, Floor View opened" + " <span class='label success'> Success</span>"); }
else{
reports.logStatus(LogStatus.FAIL, "Verify login is successful", "Login failed" + " <span class='fail label'>Fail</span>");
}
}
@AfterMethod
public void tearDown() {
reports.endTest();
driver.quit();
}
@AfterSuite
public void afterSuite(){
reports.endReport();
}
}
网格视图页面
package pages;
import base.TestBase;
import com.relevantcodes.extentreports.LogStatus;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import util.Reports;
import java.util.List;
public class GridView extends TestBase {
Reports reports;
//Page factories
@FindBy(xpath="//button[@class='pages-styles--addButton-9Gd']/span[2]")
List<WebElement> bookingSlots;
@FindBy(xpath="//ol[@class='shared-components-walkin-styles--list-3JB']/li")
List<WebElement> partySize;
@FindBy(xpath="//button[contains(text(),'Finish Reservation')]")
WebElement finishReservationBtn;
public GridView() {
PageFactory.initElements(driver, this);
reports = new Reports();
}
AddBookingView addBookingView = new AddBookingView();
public void reserveFromGridView(String pax, String slot, String table) throws InterruptedException {
//Choose Party size
for (WebElement li : partySize) {
if (li.getText().equals(pax)) {
li.click();
break;
}
}
reports.logStatus(LogStatus.INFO, "Select covers", "Covers selected" + " <span class='label success'> Success</span>");
//Choose booking time slot
for (WebElement li : bookingSlots) {
if (li.getText().equalsIgnoreCase(slot)) {
li.click();
break;
}
}
Thread.sleep(7000);
reports.logStatus(LogStatus.INFO, "Select booking slot", "Booking slot selected" + " <span class='label success'> Success</span>");
//Choose Table
addBookingView.chooseTable(table);
reports.logStatus(LogStatus.INFO, "Select table", "Table selected" + " <span class='label success'> Success</span>");
//Finish reservation
finishReservationBtn.click();
Thread.sleep(5000);
reports.logStatus(LogStatus.INFO, "Click Finish", "Clicked on Finish" + " <span class='label success'> Success</span>");
}
}
网格视图测试类
package testcases;
import base.TestBase;
import com.relevantcodes.extentreports.LogStatus;
import org.testng.annotations.*;
import pages.FloorView;
import pages.GridView;
import pages.LoginPage;
import util.Reports;
import util.TestUtil;
public class GridViewTest extends TestBase {
LoginPage loginPage;
FloorView floorView;
GridView gridView;
String sheetName = "gridview";
Reports reports;
@BeforeMethod
//Browser set up and log in to the application
public void setUp() throws InterruptedException{
initialization();
loginPage = new LoginPage();
reports = new Reports();
floorView = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
Thread.sleep(5000);
}
@DataProvider
public Object[][] CactusTestData(){
Object data[][] = TestUtil.getTestData(sheetName);
return data;
}
@Test(priority=14, dataProvider="CactusTestData")
public void reserveFromGridViewTest(String pax, String bookingTime, String table) throws InterruptedException {
// Start Extent Report
reports.startTest("Grid View Test","Launch the application and make a reservation from Grid view");
gridView = loginPage.navigateToGridView();
reports.logStatus(LogStatus.INFO, "Navigate to Grid View", "Opened grid view" + " <span class='label success'> Success</span>");
gridView.reserveFromGridView(pax,bookingTime,table);
reports.logStatus(LogStatus.PASS, "Finish reservation", "Reservation is made" + " <span class='label success'> Success</span>");
}
//Quit browser
@AfterMethod
public void tearDown()
{
reports.endTest();
driver.quit();
}
}
因此,报告是这样生成的。登录页面测试中有重复的日志行。Click to view the report generated
答案 0 :(得分:0)
这是因为您的两个测试类都调用了此登录方法。 登录页面测试类- floorView = loginPage.login(prop.getProperty(“ username”),prop.getProperty(“ password”)))
网格视图测试类-@BeforeMethod floorView = loginPage.login(prop.getProperty(“ username”),prop.getProperty(“ password”)))
如果您尝试独立运行这些测试,那么您将在单独的“范围”报告中启动单独的范围测试节点。