【问题标题】:Parallel execution does not work in Selenium Page object Model并行执行在 Selenium 页面对象模型中不起作用
【发布时间】:2020-02-21 11:11:18
【问题描述】:

我正在使用 Selenium 进行自动化 - 页面对象模型 & TestNG & Java。我在 2 个类文件中有 2 个测试用例,我想并行运行我的所有测试 我将驱动程序和测试对象传递给我的所有页面。 当我的 testng.xml 使用 parallel="classes" 打开两个浏览器窗口并且仅在一个窗口中运行时,测试正在运行。另一个窗口关闭。 Eclipse 向我展示了空指针异常。 回答: 后来我明白这是关于在 ExtentReport 初始化中使用静态的。我在记者课上更正了我的代码。

测试类 1:

public class CreateXCase extends SeleniumBase
{
    @BeforeTest(alwaysRun=true )
    public void setTestDetails()
    {
        author = System.getProperty("user.name"); 
        testcaseName="Verify that X case is successfully created";
        testcaseDec = "Create X Case";
        category="Regression";
    }


    @Test(priority=2,groups = {"Regression"})
    public  void createCaseWithNewParticipants() throws Exception
    {
        new LoginPage(driver, test).login().quickNavigation_CASE()
        .navigateToCreateCase().enterApplicantInformation()
        .navigateToCaseSection().createCase();
    }
}

测试类 2:

public class CreateYcase extends SeleniumBase
{
    @BeforeTest(alwaysRun=true )
    public void setTestDetails()
    {
        author = System.getProperty("user.name"); 
        testcaseName="Verify that Y case is successfully created";
        testcaseDec = "Create Y Case";
        category="Regression";
    }

     @Test(priority=1,groups = {"Regression"})
     public  void createCaseWithNewParticipants() throws Exception
     {
        new LoginPage(driver,test).login().quickNavigation_CASE()
        .navigateToCreateCase().enterApplicantInformation()
        .navigateToCaseSection().createCase();
    }

SeleniumBase.java:

public class SeleniumBase extends Reporter implements Browser, Element 
{

    public static WebDriverWait wait;
    @BeforeMethod(alwaysRun=true )
    public void configureAndLaunch() throws IOException 
    {           
        driver = startApp(ReadPropertyFile.get("Browser"), ReadPropertyFile.get("URL"));
    }

    @Override
    @AfterMethod (alwaysRun=true )
    public void closeBrowser()
    {
        driver.quit();
    }


@Override
    public RemoteWebDriver startApp(String browser, String url) 
    {
        try 
        {
            if (browser.equalsIgnoreCase("chrome")) 
            {
                System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
                driver = new ChromeDriver();

            } 
            else if (browser.equalsIgnoreCase("firefox")) 
            {
                System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");
                driver = new FirefoxDriver();

            } 
            else if (browser.equalsIgnoreCase("ie")) 
            {
                System.setProperty("webdriver.ie.driver", "./drivers/IEDriverServer.exe");
                driver = new InternetExplorerDriver();

            }
            driver.get(url);

            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            return driver;

        } 
        catch (Exception e) 
        {
            int a = e.toString().indexOf("Exception:");
            String str = e.toString().substring(0, a + 30);
            System.out.println(str + "Exception captured");

            System.err.println("The Browser Could not be Launched. Hence Failed");

        } 
        finally 
        {
            takeSnap();

        }
        return null;
    } 

Reporter.java:

public abstract class Reporter {

    public  RemoteWebDriver driver;

    public  ExtentHtmlReporter reporter;
    public  static ExtentReports extent;
    public  ExtentTest test;
    public String testcaseName, testcaseDec, author ; 
    public String category="";
    public static  String excelFileName;
    public static String extentreportpath;

    @BeforeSuite (alwaysRun=true )
    public void startReport(ITestContext c) throws IOException 
    {
        String reportName=this.getClass().getName().substring(29, 33).toUpperCase() +" Screen Test Report";
        String screenName=this.getClass().getName().substring(29, 33).toUpperCase() +" Tests";
        String rptName="h5{font-size: 0px;}h5::after{content:\'"+screenName+"\';font-size: 1.64rem; line-height: 110%;margin: 0.82rem 0 0.656rem 0;}";
        String suiteName = c.getCurrentXmlTest().getSuite().getName();
        if (suiteName.contains("Default suite")||suiteName.contains("Failed suite"))
        suiteName ="";
        extentreportpath="./reports/"+suiteName+"Report.html";
        reporter = new ExtentHtmlReporter(extentreportpath);
        reporter.setAppendExisting(true);       
        extent   = new ExtentReports(); 
        extent.attachReporter(reporter);
        reporter.loadXMLConfig(new File("./Resources/extent-config.xml"));
        reporter.config().setTheme(Theme.DARK);
        reporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
        reporter.config().setReportName(reportName);
        reporter.config().setCSS(rptName);
        }

    @BeforeClass(alwaysRun=true )
    public ExtentTest report()  
    {
        test = extent.createTest(testcaseName, testcaseDec);
        test.assignAuthor(author);
        return test;  
    }



    public abstract long takeSnap();

    public void reportStep(String desc,String status,boolean bSnap)
    {
        MediaEntityModelProvider img=null;
        if(bSnap && !status.equalsIgnoreCase("INFO"))
        {
            long snapNumber=100000L;
            snapNumber=takeSnap();
            try
            {
                img=MediaEntityBuilder.createScreenCaptureFromPath("./../reports/images/"+snapNumber+".jpg").build();
            }
            catch(IOException e)
            {

            }
        }
        if(status.equalsIgnoreCase("pass"))
        {
            //test.pass(desc,img);
            test.log(Status.PASS, desc, img);

        }
        else if(status.equalsIgnoreCase("fail"))
        {

            //test.fail(desc,img);

            test.log(Status.FAIL, desc, img);
        }
        else if(status.equalsIgnoreCase("INFO"))
        {
            //test.pass(desc);
            test.log(Status.INFO, desc,img);
        }
    }

    public void reportStep(String desc,String status)
    {

        reportStep(desc,status,true);
    }


    @AfterSuite (alwaysRun=true )
    public void stopReport() throws Exception 
    {
        extent.flush();
    }
}

【问题讨论】:

  • 您已经尝试过哪些更改?
  • @LeyonGudinho 如果我们设为静态,它将不会并行运行。为了能够并行运行,我们必须保持它不是静态的。
  • @Arun 这与您的 ExtentReport 变量有关,而不是 webdriver,因此您可以尝试将 ExtentReports 范围设为静态。我明天会对此进行调查,但同时尝试将报告设为静态。
  • 将 ExtentReports 设为静态应该可以解决您的问题
  • 如果您仍然遇到问题,请使用常用的东西。使用当前本地线程和 ExtentReportsManager 进行范围测试 - github.com/swtestacademy/ExtentReportsExample 或 github.com/vikas-thange/ExtentReports-TestNg-Integration 或 github.com/djangofan/extent-reporting-example

标签: java selenium selenium-webdriver testng


【解决方案1】:

将 ExtentReport 变量“extent”设为静态解决了我的问题。 例如:public static ExtentReports extent; 我也修改了问题中的代码。

由于我在一个类中只有一个 @Test,因此 parallel="methods" 在我的情况下没有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多