簡體   English   中英

並行執行在 Selenium Page 對象模型中不起作用

[英]Parallel execution does not work in Selenium Page object Model

我正在使用 Selenium - Page object Model & TestNG & Java 進行自動化。 我在 2 個類文件中有 2 個測試用例,我想並行運行所有測試參考圖片 我將驅動程序和測試對象傳遞給我的所有頁面。 當使用 parallel="classes" 運行我的 testng.xml 時,兩個瀏覽器窗口打開並且僅在一個窗口中運行,測試正在運行。 另一個窗口是關閉的。 Eclipse 向我展示了空指針異常。 答:后來我明白這是關於在 ExtentReport 初始化中使用 static 的。 我在記者課上更正了我的代碼。

測試類 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;
    } 

記者.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();
    }
}

將 ExtentReport 變量“extent”設為靜態解決了我的問題。 例如: public static ExtentReports extent; 我也修改了我的問題中的代碼。

由於我在一個班級中只有一個 @Test,parallel="methods" 在我的情況下沒有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM