Apache Ant' Build.xml' >由于' @ BeforeClass'而导致失败

时间:2016-11-01 10:44:50

标签: java selenium selenium-webdriver ant webdriver

Apache Ant' Build.xml' >由于' @ BeforeClass'

而失败

不知道为什么我的构建文件没有执行我的测试? 由于以下原因,似乎失败了:

控制台例外: enter image description here

构建文件:

<project name="Sample Ant build" default="testng-execution" basedir="C:/Users/gpb7642/Desktop/PhAutomationFramework/PhFramework">
<!-- ========== Initialize Properties =================================== -->
<!-- set global properties for build -->
<property name="basedir" value="." />

<!-- created lib folder to store specifc jar files-->
<property name="lib" value="${basedir}/lib" />

<!-- contains all java files ending with .java-->
<property name="src" value="${basedir}/src" />

<!-- contains all classes ending with .class -->
<property name="bin" value="${basedir}/bin" />

<!-- creates the directory for the reports -->
<property name="report-dir" value="${basedir}/Test-Report" />
<!-- will create a folder within the report directory -->
<property name="testng-report-dir" value="${report-dir}/TestNGreport" />


<!-- ====== Set the classpath ====  -->
<path id="classpath">
    <pathelement location="${bin}" />
    <fileset dir="${lib}">
        <include name="*.jar" />
    </fileset>
</path>

<!-- Delete directories  -->
<target name="delete-dir">
    <!--delete the two directories listed below -->
    <delete dir="${bin}" />
    <delete dir="${report-dir}" />
</target>

<!-- Creating directories -->
<target name="create" depends="delete-dir">
    <!--create the two directories listed below -->
    <mkdir dir="${bin}" />
    <mkdir dir="${report-dir}" />
</target>

<!-- Compile the java code from ${src} into ${bin}
Will create the relevant class files and send them all into the bin directory in format: .class -->
<target name="compile" depends="create">
    <javac srcdir="${src}" classpathref="classpath" includeAntRuntime="No" destdir="${bin}" />
    <echo> /* Compiled Directory Classes */ </echo>
</target>

<!-- Runs the file and generates Reportng report for TestNG-->
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="classpath" />

<target name="testng-execution" depends="compile">
    <mkdir dir="${testng-report-dir}" />
    <testng outputdir="${testng-report-dir}" classpathref="classpath" useDefaultListeners="true">
        <xmlfileset dir="${basedir}" includes="testng.xml" />
    </testng>
</target>

浏览器工厂:

    @BeforeClass (alwaysRun = true)
public void setup() throws Exception {
    basePage = PageFactory.initElements(BrowserFactory.getDriver(), BasePage.class);
    pageHeader = PageFactory.initElements(BrowserFactory.getDriver(), PageHeader.class);
    phHomepage = PageFactory.initElements(BrowserFactory.getDriver(), Homepage.class);
    signInPage = PageFactory.initElements(BrowserFactory.getDriver(), SignInPage.class);
    hutLocalisePage = PageFactory.initElements(BrowserFactory.getDriver(), HutLocalisePage.class);
    pizzaPage = PageFactory.initElements(BrowserFactory.getDriver(), PizzaPage.class);
    excelSheet = PageFactory.initElements(BrowserFactory.getDriver(), ExcelReader.class);
    yourOrderPage = PageFactory.initElements(BrowserFactory.getDriver(), YourOrderPage.class);
    checkoutDetailsPage = PageFactory.initElements(BrowserFactory.getDriver(), CheckoutDetailsPage.class);
    paymentInformationPage = PageFactory.initElements(BrowserFactory.getDriver(), PaymentInformationPage.class);
    thanksPage = PageFactory.initElements(BrowserFactory.getDriver(), ThanksPage.class);
    screenshot = PageFactory.initElements(BrowserFactory.getDriver(), Screenshot.class);
    basePage.packageName = this.getClass().getPackage().toString();
}


// Make sure driver instance is unassigned once tasks are completed
@AfterClass (alwaysRun = true)
public void quitDriver() throws Exception {
    if (null != webdriver) {
        getDriver().manage().deleteAllCookies();
        webdriver.quit();
        webdriver = null;
    }
}

@AfterMethod (alwaysRun = true)
public void takeScreenShotOnFailure(ITestResult testResult) throws WebDriverException, Exception {
    if (testResult.getStatus() == ITestResult.FAILURE) {
        System.out.println(testResult.getStatus());

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
        String time = dateFormat.format(now);
        new File("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\FailedTests\\" + time).mkdir();

        String timeStamp = new SimpleDateFormat("HH.mm.SS").format(Calendar.getInstance().getTime());

        File scrFile = ((TakesScreenshot) BrowserFactory.getDriver()).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File(("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\FailedTests\\" + time + "\\") + timeStamp.toString() + ", " + basePage.packageName + ", " +  this.getClass().getSimpleName().toString() + "," + testResult.getName() +  ".jpeg"));
        //FileUtils.copyFile(scrFile, new File(("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\FailedTests\\" + time + "\\") + time + ", " + basePage.packageName + ", "  + this.getClass().getSimpleName().toString() + "_" + testResult.getName() + ".jpeg"));
    }
}


@AfterMethod (alwaysRun = true)
public void takeScreenShotOnSuccessfulOrderConfirmation(ITestResult testResult) throws WebDriverException, Exception {
   WebDriverWait wait;
   wait = new WebDriverWait(getDriver(), 10);

    if (testResult.getMethod().getMethodName().toString().contains("validateOrderWasSuccessful")) {
        if(testResult.getStatus() == ITestResult.SUCCESS) {
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy");
            String time = dateFormat.format(now);
            new File("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PassedTests\\" + time).mkdir();

            String timeStamp = new SimpleDateFormat("HH.mm.SS").format(Calendar.getInstance().getTime());

            //https://www.uat.pizzahut.co.uk/thanks?orderReference=aPiZ3Fee1Y0%3D
            wait.until(ExpectedConditions.urlContains("https://www.uat.pizzahut.co.uk/thanks?"));

            File scrFile = ((TakesScreenshot) BrowserFactory.getDriver()).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File(("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\PassedTests\\" + time + "\\") + timeStamp.toString() + ", " + basePage.packageName + ", " +  this.getClass().getSimpleName().toString() + "," + testResult.getName() +  ".jpeg"));
            //FileUtils.copyFile(scrFile, new File(("C:\\Users\\gpb7642\\Desktop\\PhAutomationFramework\\FailedTests\\" + time + "\\") + time + ", " + basePage.packageName + ", "  + this.getClass().getSimpleName().toString() + "_" + testResult.getName() + ".jpeg"));  
        }
    }
}

    @AfterMethod (alwaysRun = true)
    //On Test Pass: LOG: Class Name & Method Name
    public void onTestSuccess(ITestResult tr){
        if(tr.getStatus() == ITestResult.SUCCESS) {
        DOMConfigurator.configure("log4j.xml");
        Log.info("@PASS: Package & Class: " + tr.getTestClass().getName().toString() + ",  " + "Method: " + tr.getName().toString());
        }
    }

    @AfterMethod (alwaysRun = true)
    //On Test Fail: LOG: Class Name & Method Name 
    public void onTestFailure(ITestResult tr) {
        if(tr.getStatus() == ITestResult.FAILURE) {
        DOMConfigurator.configure("log4j.xml");
        Log.info("@FAIL: Package & Class: "  + tr.getTestClass().getName().toString() + ",  "  + "Method: " + tr.getName().toString());
    }
    }

    @AfterMethod (alwaysRun = true)
    //On Test Skipped: LOG: Class Name & Method Name
    public void onTestSkipped(ITestResult tr) {
        if(tr.getStatus() == ITestResult.SKIP) {
        DOMConfigurator.configure("log4j.xml");
        Log.info("@SKIPPED: Package & Class: " + tr.getTestClass().getName().toString() + ",  "+ "Method: "  + tr.getName().toString());
        }
    }

TestNG文件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="PH_Automation_Scripts_by" verbose="2">
    <test name="PH Automation Tests: Pizza Combinations">
       <classes>
            <class name="PhFramework.pizzas.chickenSupreme.glutenFree.MediumTest"></class>
       </classes>
    </test>
</suite>

1 个答案:

答案 0 :(得分:1)

jar xmlbeans.jar必须位于类路径中。您可以从此处下载最新版本:https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/2.6.0

build.xml正在将dir ./lib/中的其他jar添加到类路径中。