具有多个类的范围报告

时间:2017-07-13 07:35:35

标签: java selenium report

来自不同类的所有测试用例都没有与范围报告一起运行。 范围报告中的一个类 二等忘记了密码 第三类是登录 我想同时运行两个类登录和密码,并希望为这两个类创建单个报告

       package Pedagogy;

        import java.util.ArrayList;
        import java.util.List;
        import java.util.concurrent.TimeUnit;
        import org.openqa.selenium.By;
        import org.openqa.selenium.Keys;
        import org.openqa.selenium.WebElement;
        import org.testng.annotations.Test;
        import com.relevantcodes.extentreports.LogStatus;
        import common.generalmail2;




        public class forgotPassword extends generalmail2{

            ArrayList<String> pathlist=new ArrayList<String>();
            String btn = "";
            String path= "";
            generalmail2  generalMail = new generalmail2();



            @Test
            public void Regestration_test() throws Exception {
                Thread.sleep(2000);
                logger = report.startTest("ForgotPassword");
                getDriver().findElement(By.id("forgotpassword")).click();
                getLogger().log(LogStatus.PASS ,"got the forgot password id");
                getLogger().log(LogStatus.INFO , "forgotpassword page is open");
                validationTest("Sheet3");

            }

            public void validationTest(String sheet) throws Exception {

                Selenium_read obj = new Selenium_read();
                ArrayList<ArrayList> tablelist=obj.readExcel(sheet);
                System.out.println(tablelist);
                for (ArrayList<String> row : tablelist){
                    Forgotpassword_method(row.get(0),row.get(1));
                }

                Forgotpassword_method1();
                Forgotpassword_method2();       
            }

            public void Forgotpassword_method(String email, String snapshot)throws Exception {


                WebElement email_id= getDriver().findElement(By.xpath(".//*[@id='forgotPasswordForm']/div[1]/div/input"));
                email_id.clear();
                String myWindowHandle = getDriver().getWindowHandle();
                getDriver().switchTo().window(myWindowHandle);
                WebElement email_id1= getDriver().findElement(By.xpath(".//*[@id='forgotPasswordForm']/div[1]/div/input"));
                email_id1.sendKeys(email);
                getDriver().findElement(By.xpath(".//*[@id='forgotPasswordForm']/div[2]/div/button[2]")).click();

            }

            public void Forgotpassword_method1()throws Exception {

                getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                getDriver().manage().window().maximize();
                getDriver().get("https://mail.google.com/");
                getDriver().findElement(By.id("identifierId")).sendKeys("kunal@pedagogy.study");
                getDriver().findElement(By.xpath(".//*[@id='identifierNext']/content/span")).click();
                Thread.sleep(4000);
                getDriver().findElement(By.xpath(".//*[@id='password']/div[1]/div/div[1]/input")).sendKeys("pedagogy@123");
                Thread.sleep(4000);
                getDriver().findElement(By.id("passwordNext")).click();
                Thread.sleep(4000);
                getLogger().log(LogStatus.INFO , "successfully open email");
                List<WebElement> email = getDriver().findElements(By.cssSelector("div.xT>div.y6>span>b"));
                for(WebElement emailsub : email){
                if(emailsub.getText().equals("Pedagogy : Password Reset") == true){

                       emailsub.click();
                       break;
                    }
                }
                Thread.sleep(4000);
                String mytext = getDriver().findElement(By.partialLinkText("http://52.89.55.95:3000/changepassword?email=kunal@pedagogy.study&token=")).getText();
                getDriver().get(mytext);
            }

            public void Forgotpassword_method2()throws Exception {

                getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                getDriver().findElement(By.xpath(".//*[@id='password']")).sendKeys("Pedagogy@123");
                getDriver().findElement(By.xpath(".//*[@id='confirmPassword']")).sendKeys("Pedagogy@123");
                getDriver().findElement(By.xpath("html/body/app-root/app-changepassword/div/div/div[2]/form/div[3]/button")).click();
                getLogger().log(LogStatus.INFO , "Successfully change the password");


            }


            public void switchToTab() { 
                getDriver().findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
                getDriver().switchTo().defaultContent(); 
            }



            public void setButton(String str){
                btn=str;
            }


            public void setPath(String str){
                path=str;
            }

        }



    package Pedagogy;


    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.ui.Select;
    import org.testng.annotations.Test;
    import common.generalmail2;




    public class Registration extends generalmail2 {

            ArrayList<String> pathlist=new ArrayList<String>();
            String btn = "";
            String path= "";
            generalmail2  generalMail = new generalmail2();



        @Test
        public void Regestration_test() throws Exception {
            logger = report.startTest("SignUp");
            getDriver().findElement(By.xpath(".//*[@id='wrapper']/div/div/div/a")).click();
            validationTest("Sheet1");

            }


        public void regNull() {
                getDriver().findElement(By.id("firstname")).clear();
                getDriver().findElement(By.id("lastname")).clear();
                getDriver().findElement(By.xpath("html/body/app-root/app-register/div/div/div/div[2]/form/div[3]/ng2-datepicker/div/div/input")).clear();
                getDriver().findElement(By.name("radio1")).isSelected();
                Select dropdown = new Select(getDriver().findElement(By.id("countrycode")));dropdown.selectByVisibleText("India");
                getDriver().findElement(By.id("contact")).clear();
                getDriver().findElement(By.id("email")).clear();
                getDriver().findElement(By.id("agree")).click();
                getDriver().findElement(By.id("submitBtn")).click();

            }   

            public void validationTest(String sheet) throws Exception{
                // Null Validations
                regNull();

                Selenium_read obj = new Selenium_read();
                ArrayList<ArrayList> tablelist=obj.readExcel(sheet);
                System.out.println(tablelist);
                for (ArrayList<String> row : tablelist){
                    regMethod(row.get(0),row.get(1),row.get(2),row.get(3),row.get(4),row.get(5));
                }

                registration_method();
                Createpassword_method();
            }


            public void regMethod(String firstName ,String lastName ,String dob, String contact,String email , String snapshot ) throws Exception {
                regNull();
                Thread.sleep(4000);
                getDriver().findElement(By.id("firstname")).sendKeys(firstName);
                getDriver().findElement(By.id("lastname")).sendKeys(lastName);
                getDriver().findElement(By.xpath("html/body/app-root/app-register/div/div/div/div[2]/form/div[3]/ng2-datepicker/div/div/input")).sendKeys(dob);
                getDriver().findElement(By.id("one")).isEnabled();
                getDriver().findElement(By.id("contact")).sendKeys(contact);
                getDriver().findElement(By.id("email")).sendKeys(email);
                getDriver().findElement(By.id("agree")).click();
                getDriver().findElement(By.id("submitBtn")).click();
                snapshot(snapshot);
                regNull();
            }
            public void registration_method()throws Exception {

                getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
                getDriver().manage().window().maximize();
                getDriver().get("https://mail.google.com/");
                getDriver().findElement(By.id("identifierId")).sendKeys("kunal@pedagogy.study");
                getDriver().findElement(By.xpath(".//*[@id='identifierNext']/content/span")).click();
                Thread.sleep(4000);
                getDriver().findElement(By.xpath(".//*[@id='password']/div[1]/div/div[1]/input")).sendKeys("pedagogy@123");

                Thread.sleep(4000);
                getDriver().findElement(By.id("passwordNext")).click();

                Thread.sleep(4000);
                List<WebElement> email = getDriver().findElements(By.cssSelector("div.xT>div.y6>span>b"));

                for(WebElement emailsub : email){
                    if(emailsub.getText().equals("Pedagogy : Password Reset") == true){

                           emailsub.click();
                           break;
                        }
                }

                Thread.sleep(4000);

                String mytext = getDriver().findElement(By.partialLinkText("http://52.89.55.95:3000/changepassword?email")).getText();
                 getDriver().get(mytext);
                }


            public void Createpassword_method()throws Exception {
                getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                getDriver().findElement(By.xpath(".//*[@id='password']")).sendKeys("Pedagogy@123");
                getDriver().findElement(By.xpath(".//*[@id='confirmPassword']")).sendKeys("Pedagogy@123");
                getDriver().findElement(By.xpath("html/body/app-root/app-changepassword/div/div/div[2]/form/div[3]/button")).click();


            }


            public void setButton(String str){
                btn=str;
            }

            public void setPath(String str){
                path=str;
            }

            public void snapshot(String name) throws IOException {
                File screenshot = ((TakesScreenshot)getDriver()).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(screenshot, new File("D:\\PEDAGOGY SELENIUM PROJECT\\selenium\\screenshot signup\\"+path+name));
                pathlist.add("D:\\PEDAGOGY SELENIUM PROJECT\\selenium\\screenshot\\"+path+name);


        }
        }





<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">

   <test name="Test2">
    <classes>
     <class name="Pedagogy.login"/>
      <class name="Pedagogy.forgotPassword"/>
      <class name="Pedagogy.Registration"/>
    </classes>


   </test>



</suite> 


package common;

import static org.testng.Assert.assertTrue;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class generalmail2 {

    public ExtentReports report=new ExtentReports("D:\\KETAN\\ProjectReport.html");;
    public static ExtentTest logger;



    public ExtentTest getLogger() {
        return logger;
    }


    public void setLogger(ExtentTest logger) {
        this.logger = logger;
    }


    WebDriver driver ;
    public WebDriver getDriver() {
        return driver;
    }


    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }


    @BeforeSuite
    public void OpenBrowser()
    {
        System.setProperty("webdriver.chrome.driver", "D://KETAN//selenium//chromedriver_win32//chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        logger = report.startTest("Pedagogy");
        driver.get("http://52.89.55.95:3000/");
        logger.log(LogStatus.INFO , "Appliacation is up and running");


    }


    public ExtentReports getReport() {
        return report;
    }


    public void setReport(ExtentReports report) {
        this.report = report;
    }


    @AfterMethod
    public void tearDown(ITestResult result) 
    {
        if (result.getStatus()==ITestResult.FAILURE)
        {
        String screenshotpath = generalScreenshot.captureScreenshot(driver, result.getName());
        String image = logger.addScreenCapture(screenshotpath);
        logger.log(LogStatus.FAIL , "failed", image);   
        }

        report.endTest(logger);
        report.flush();


    }


    @AfterSuite
    public void openReport(){
        driver.get("D:\\KETAN\\ProjectReport.html");

    }

}

2 个答案:

答案 0 :(得分:1)

您需要创建一个单独的类来声明与范围报告相关的类,并且需要扩展该类以访问每个类中需要为测试方法生成报告的那些类的对象。 我已经创建了一个类(即BaseTest)来声明与范围报告相关的类,现在我将再创建2个类(即ClassA和ClassB)来访问这些对象,并从这两个类中创建单个报告。

这是BaseTest类:-

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.ChartLocation;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class BaseTest{

public static ExtentHtmlReporter htmlReporter;
public static ExtentReports extent;
public static ExtentTest test;

@BeforeSuite
public void setUp()
{
    htmlReporter = new ExtentHtmlReporter(System.getProperty("user.dir") +"/test-output/extentReport.html");
    extent = new ExtentReports();
    extent.attachReporter(htmlReporter);

    extent.setSystemInfo("OS", "Mac Sierra");
    extent.setSystemInfo("Host Name", "Test");
    extent.setSystemInfo("Environment", "QA");
    extent.setSystemInfo("User Name", "Napendra Singh");

    htmlReporter.config().setChartVisibilityOnOpen(true);
    htmlReporter.config().setDocumentTitle("Extent report");
    htmlReporter.config().setReportName("Final Report");
}

@AfterMethod
public void getResult(ITestResult result)
{
    if(result.getStatus() == ITestResult.FAILURE)
    {
        test.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED));
        test.fail(result.getThrowable());
    }
    else if(result.getStatus() == ITestResult.SUCCESS)
    {
        test.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN));
    }
    else
    {
        test.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE));
        test.skip(result.getThrowable());
    }
}

@AfterSuite
public void tearDown(){
    extent.flush();
   }
}

这是ClassA类:-

import org.testng.Assert;
import org.testng.annotations.Test;

public class ClassA extends BaseTest{

@Test
public void classA(){
    //Add your implementation
  }
}

这是ClassB类别:-

import org.testng.Assert;
import org.testng.annotations.Test;

public class ClassB extends BaseTest{

@Test
public void classB(){
   //Add your implementation
   }
}

现在,您可以使用testng.xml文件运行这两个类,并将获得这两个类的合并报告。

答案 1 :(得分:0)

您使用的是哪种版本的范围报告?根据您的代码判断,它不是第三个。但无论如何,你应该在@AfterSuite中调用report.flush()而不是@AfterMethod。