Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 并行执行在Selenium页面对象模型中不起作用_Java_Selenium_Selenium Webdriver_Testng - Fatal编程技术网

Java 并行执行在Selenium页面对象模型中不起作用

Java 并行执行在Selenium页面对象模型中不起作用,java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,我正在使用Selenium-Page对象模型、TestNG和Java实现自动化。我在2个类文件中有2个测试用例,我想并行运行所有测试 我将驱动程序和测试对象传递到我的所有页面。 当在parallel=“classes”打开两个浏览器窗口且仅在一个窗口中运行my testng.xml时,测试正在运行。另一扇窗户关着。Eclipse向我显示了空指针异常。 答复: 后来我了解到这是关于在ExtentReport初始化中使用静态的。我在记者课上修改了我的代码 1级测试: 2级测试: SeleniumBa

我正在使用Selenium-Page对象模型、TestNG和Java实现自动化。我在2个类文件中有2个测试用例,我想并行运行所有测试 我将驱动程序和测试对象传递到我的所有页面。 当在parallel=“classes”打开两个浏览器窗口且仅在一个窗口中运行my testng.xml时,测试正在运行。另一扇窗户关着。Eclipse向我显示了空指针异常。 答复: 后来我了解到这是关于在ExtentReport初始化中使用静态的。我在记者课上修改了我的代码

1级测试: 2级测试: SeleniumBase.java: Reporter.java:
将ExtentReport变量“extent”作为静态变量解决了我的问题。 例如:
公共静态扩展端口范围
我还修改了问题中的代码


因为我在一个类中只有一个@Test,parallel=“methods”在我的情况下没有任何用处。

将ExtentReport变量“extent”作为静态变量解决了我的问题。 例如:
公共静态扩展端口范围
我还修改了问题中的代码


因为我在一个类中只有一个@Test,parallel=“methods”在我的情况下没有任何用处。

您已经尝试了哪些更改?@leyongudino如果我们创建static,它将不会并行运行。为了能够并行运行,我们必须使其保持非静态。@Arun这与您的ExtentReport变量有关,而不是webdriver,因此您可以尝试使ExtentReports扩展为静态。明天我将对此进行研究,但同时尝试将报表设置为静态。将ExtentReports设置为静态应该可以解决您的问题。如果您仍然面临此问题,请使用流行的方法。使用当前本地线程和ExtentReportsManager进行扩展测试-github.com/swtestacademy/extentreportsesample或github.com/vikas-thange/ExtentReports-TestNg-Integration或您已经尝试过的更改是什么?@leyongudino如果我们使用static,它将不会并行运行。为了能够并行运行,我们必须使其保持非静态。@Arun这与您的ExtentReport变量有关,而不是webdriver,因此您可以尝试使ExtentReports扩展为静态。明天我将对此进行研究,但同时尝试将报表设置为静态。将ExtentReports设置为静态应该可以解决您的问题。如果您仍然面临此问题,请使用流行的方法。使用当前本地线程和ExtentReportsManager的数据块测试-github.com/swtestacademy/ExtentReportsExample或github.com/vikas-thange/ExtentReports-TestNg-Integration或
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();
    }
}
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();
    }
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;
    } 
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();
    }
}