testNG XML文件不考虑我的第二个类只运行我的第一个类,第二个类显示java.lang.NullPointerException

时间:2017-09-28 08:12:32

标签: java selenium-webdriver testng

当我运行XML时,第一个类正在运行。第一类包含@beforeTest@AfterTest注释。我的@Test注释是在第二类,但它没有运行。

<suite name = "smsweb"> 
    <test name = "login">
        <classes>
            <class name = "testNG.datapro" /> //Running
        </classes>
    </test> 
    <test name = "form">
        <classes>           
            <class name = "testNG.assetscat" /> //not running
        </classes>
    </test> 
</suite>

- 第一类“datapro”

package testNG;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class datapro {
    WebDriver driver;

    @BeforeTest
    public void webopen() throws InterruptedException {

        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\saad bin usman\\Downloads\\Compressed\\chromedriver_win32_2\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        Actions a = new Actions(driver);
        driver.get("http://test.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        SoftAssert softassert = new SoftAssert();

        String title = driver.getTitle();
        String exptitle = "Cloud Tenants - Sales Management System";

        String user = "admin";
        String pass = "admin";

        Assert.assertEquals(title, exptitle);

        String url = driver.getCurrentUrl();

        driver.findElement(By.xpath(".//*[@id='txtUsername']")).clear();
        driver.findElement(By.xpath(".//*[@id='txtUsername']")).sendKeys(user);

        driver.findElement(By.xpath(".//*[@id='txtPassword']")).clear();
        driver.findElement(By.xpath(".//*[@id='txtPassword']")).sendKeys(pass);

        Thread.sleep(3000);
        driver.findElement(By.xpath(".//*[@id='section-login']/div/div/div[4]/button")).click();
        Thread.sleep(4000);

        String expectedurl = driver.getCurrentUrl();
        Assert.assertNotEquals(url, expectedurl);

        String formname = "asset group";
        Thread.sleep(6000);
        driver.findElement(By.xpath(".//div[@class='sidebar-toggler hidden-phone']")).click();
        driver.findElement(By.xpath(".//*[@id='txtMenuSearch']")).clear();
        driver.findElement(By.xpath(".//*[@id='txtMenuSearch']")).sendKeys(formname);
        Thread.sleep(2000);
        WebElement searcharea = driver.findElement(By.xpath(".//ul[@class='page-sidebar-menu']"));
        WebElement abc = searcharea.findElement(By.tagName("ul"));
        List<WebElement> xyz = abc.findElements(By.tagName("a"));

        System.out.println("No of Count Related Your Search " + xyz.size());
        int count = 0;
        for (int i = 0; i < xyz.size(); i++) {
            if (formname.equalsIgnoreCase(xyz.get(i).getText())) {
                System.out.println("Form Name Which System Open " + xyz.get(i).getText());
                count++;
                xyz.get(i).click();
                break;
            }
        }
        if (count < 1) {
        }

        WebElement formpagename = driver
                .findElement(By.xpath(".//div[@class='portlet-title']/div[@class='caption span6 text-left']"));
        softassert.assertEquals(formpagename.getText().equalsIgnoreCase(formpagename.getText()),
                formname.equalsIgnoreCase(formpagename.getText()));

        Thread.sleep(5000L);
        try {
            driver.findElement(By.xpath(".//*[@id='btnAddRecord']/i")).click();
            softassert.assertEquals(formpagename.getText().equalsIgnoreCase(formpagename.getText()),
                    formpagename.getText().equalsIgnoreCase(formpagename.getText()));
        } catch (Exception e) {
            System.out.println("Add New Record's Button Not Found");
        }
        softassert.assertAll();
        Thread.sleep(8000L);
    }

    @AfterTest
    public void closebrowser() {
        driver.quit();
    }

- 第二类“assetscat”

package testNG;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class assetscat {

    @Test(priority = 1)
    public void entername() throws InterruptedException {
        datapro d = new datapro();
        SoftAssert sa = new SoftAssert();

        d.driver.findElement(By.xpath(".//*[@id='btnAddEditSave']")).click();
        d.driver.findElement(By.xpath(".//*[@class='icon-remove-circle']")).click();

        System.out.println(d.driver
                .findElement(By.xpath(".//*[@id='frmAddEdit']//div[@class='control-group required error']")).getText());

        // WebElement notisyb
        // =d.driver.findElement(By.xpath(".//*[@id='errorNotificationBtn']"));
        // System.out.println(notisyb.getText());

        List<WebElement> errorelement = d.driver.findElements(By.xpath(
                ".//*[@id='frmAddEdit']//div[@class='control-group required error']/div/*[@data-dbpropertyname]"));
        System.out.println("Mendatory Fields are ");
        for (int er = 0; er < errorelement.size(); er++) {
            System.out.println(errorelement.get(er).getAttribute("data-dbpropertyname"));
        }
        for (int e = 0; e < errorelement.size(); e++) {

            // System.out.println(errorelement.get(e).getAttribute("data-dbpropertyname"));

            if (errorelement.get(e).getAttribute("data-dbpropertyname").equalsIgnoreCase("Name")) {

                WebElement savebut = d.driver.findElement(By.xpath(".//*[@id='btnAddEditSave']"));

                errorelement.get(e).sendKeys("         ");
                savebut.click();
                WebElement notipannel = d.driver.findElement(By.xpath(".//*[@class='icon-remove-circle']"));
                if (notipannel.isDisplayed()) {
                    notipannel.click();
                }

                List<WebElement> cond1 = d.driver.findElements(By.xpath(
                        ".//*[@id='frmAddEdit']//div[@class='control-group required error']/div/*[@data-dbpropertyname]"));

                sa.assertEquals(errorelement.size(), cond1.size());

                errorelement.get(e).clear();
                errorelement.get(e).sendKeys("@#$%^");
                savebut.click();
                if (notipannel.isDisplayed()) {
                    notipannel.click();
                }

                List<WebElement> cond2 = d.driver.findElements(By.xpath(
                        ".//*[@id='frmAddEdit']//div[@class='control-group required error']/div/*[@data-dbpropertyname]"));

                sa.assertEquals(errorelement.size(), cond2.size());

                errorelement.get(e).clear();
                errorelement.get(e).sendKeys("CHaracter20000CHaracter20000CHaracter20000CHaracter20000CHaracter20000");
                savebut.click();
                if (notipannel.isDisplayed()) {
                    notipannel.click();
                }

                List<WebElement> cond3 = d.driver.findElements(By.xpath(
                        ".//*[@id='frmAddEdit']//div[@class='control-group required error']/div/*[@data-dbpropertyname]"));

                sa.assertEquals(errorelement.size(), cond3.size());
                sa.assertAll();

                if (notipannel.isDisplayed()) {
                    notipannel.click();
                }
                try {
                    WebElement datasave = d.driver.findElement(By.xpath(".//*[@class='notification alert-success']"));
                    if (datasave.isDisplayed()) {
                        System.out.println("Afetr Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
                                + " Transaction Sucussfully Saved");
                        break;

                    }
                } catch (Exception e1) {
                    System.out.println("After Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
                            + " Transaction not Saved");
                }

            }

            else if (errorelement.get(e).getAttribute("data-dbpropertyname").equalsIgnoreCase("ShortName")) {
                errorelement.get(e).clear();
                errorelement.get(e).sendKeys("Sadi");
                d.driver.findElement(By.xpath(".//*[@id='btnAddEditSave']")).click();

                WebElement notipannel = d.driver.findElement(By.xpath(".//*[@class='icon-remove-circle']"));
                if (notipannel.isDisplayed()) {
                    notipannel.click();
                }
                try {
                    WebElement datasave = d.driver.findElement(By.xpath(".//*[@class='notification alert-success']"));
                    if (datasave.isDisplayed()) {
                        System.out.println("Afetr Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
                                + " Transaction Sucussfully Saved");
                        break;

                    }
                } catch (Exception e1) {
                    System.out.println("After Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
                            + " Transaction not Saved");
                }

            }

            else if (errorelement.get(e).getAttribute("data-dbpropertyname").equalsIgnoreCase("Colorcode")) {

                errorelement.get(e).sendKeys("Black");
                d.driver.findElement(By.xpath(".//*[@id='btnAddEditSave']")).click();

                WebElement notipannel = d.driver.findElement(By.xpath(".//*[@class='icon-remove-circle']"));
                if (notipannel.isDisplayed()) {
                    notipannel.click();
                }
                try {
                    WebElement datasave = d.driver.findElement(By.xpath(".//*[@class='notification alert-success']"));
                    if (datasave.isDisplayed()) {
                        System.out.println("Afetr Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
                                + " Transaction Sucussfully Saved");
                        break;

                    }
                } catch (Exception e1) {
                    System.out.println("After Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
                            + " Transaction not Saved");
                }

            }

        }
    }

}

3 个答案:

答案 0 :(得分:0)

试试这个,它应该有效。

<suite name = "smsweb"> 
    <test name = "login">
        <classes>
            <class name = "testNG.datapro" /> 
        </classes>
        <classes>           
            <class name = "testNG.assetscat" /> 
        </classes>
    </test> 
</suite>

答案 1 :(得分:0)

TestNG保证@BeforeTest带注释的方法每<test>个标签只执行一次。您还没有向我们展示您的测试代码。但是按照你的描述,我猜你的两个测试类都有@Test个方法,这些方法依赖于一个预期通过@BeforeTest方法初始化的变量,这个@BeforeTest方法可能也许一些共同基类的一部分。

如果您的用例绝对需要每<test>只进行一次初始化,那么您可能需要执行以下操作:

将初始化逻辑移动为

的一部分
  • org.testng.ITestListener - 如果您希望每个测试标记只执行一次设置(即,而不是使用@BeforeTest)
  • org.testng.ISuiteListener - 如果您希望每个套件标签只执行一次设置(即,而不是使用@BeforeSuite)
  • 使用setAttribute()方法将初始化数据持久化为ITestContext(这代表测试标记)或ISuite(这代表套件标记)的属性。
  • 通过以下方式将此监听器连接到TestNG:
    • 侦听器标记(或)
    • @Listeners注释(或)
    • 服务加载器。

您可以参考我的this博客文章以获取更多详细信息以及一些显示所有这些内容的示例。

查看来源代码后的更新

你完全弄错了。实例化类和TestNG实例化类之间存在差异。访问webdriver数据成员时assetscat.entername()失败并NullPointerException的原因是因为所有初始化都在webopen()中完成,并使用@BeforeTest注释。

要解决此问题,您有两种选择:

  1. 在尝试访问其中的webopen()对象之前,在assetscat.entername()方法中显式调用方法driver
  2. 拥有assetscat extends datapro
  3. 我还建议你花一些时间处理所有这些注释,以便更好地理解如何使用它们。

答案 2 :(得分:0)

尝试此代码,这将使两个测试都运行

<suite name = "smsweb"> 
    <test name = "login">
        <classes>

            <class name = "testNG.datapro" />  

            <class name = "testNG.assetscat" />
        </classes>
    </test> 
</suite>