使用pagefactory

时间:2017-04-14 10:42:18

标签: selenium-webdriver testng pageobjects

我正在使用testNG运行2个类。第一个类成功运行但第二个类失败并出现错误:

  

“org.testng.TestNGException:无法调用public void TestScripts.NewAccountCreation.AccountCreation1():要么将它设为静态,要么在你的类中添加一个no-args构造函数”

如果我添加非参数构造函数,我将获得空指针异常。

我正在使用Pagefactory设计我的测试用例。

Eclipse版本:kepler

TestNG的:6.9.9

Chrome版本:57.0.2987.133

Chrome驱动程序:2.27

任何建议都将不胜感激。

请找到以下代码:

页面工厂登录页面代码:

/**
 * 
 */
package Pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import Pages.LoginPage;

public class LoginPage {

    WebDriver driver;

    public LoginPage(WebDriver ldriver)

    {
        this.driver = ldriver;

    }

    @FindBy(xpath = "//input[contains(@id,'username-inputEl')]")
    public WebElement username;

    @FindBy(xpath = "//input[contains(@id,'password-inputEl')]")
    public WebElement password;

    @FindBy(xpath = "//span[contains(@id,'submit-btnInnerEl')]")
    public WebElement LoginButton;

    // Methods to perform actions

    public void pCLogin(String Username, String Password, WebDriver driver)

    {

        username.sendKeys(Username);

        password.sendKeys(Password);

        LoginButton.click();

    }

}

页面帐户创建页面的工厂代码:

package Pages;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Accounttab {

    WebDriver driver;

    public Accounttab(WebDriver ldriver)

    {
        this.driver = ldriver;

    }

    @FindBy(xpath = "//span[@id='TabBar:AccountTab-btnWrap']")
    public WebElement accountDropDown;

    @FindBy(xpath = "//span[contains(@id,'AccountTab_NewAccount-textEl')]")
    public WebElement NewAccount_Button;

    @FindBy(xpath = "//textarea[contains(@id,'GlobalContactNameInputSet:Name-inputEl')]")
    public WebElement Companyname;

    @FindBy(xpath = "//input[contains(@id,'OfficialIDDV_Input-inputEl')]")
    public WebElement FEINNumber;

    @FindBy(xpath = "//input[contains(@id,'GlobalAddressInputSet:City-inputEl')]")
    public WebElement City;

    @FindBy(xpath = "//input[contains(@id,'GlobalAddressInputSet:PostalCode-inputEl')]")
    public WebElement Zipcode;

    @FindBy(xpath = "//a[contains(@id,'SearchLinksInputSet:Search')]")
    public WebElement AccountsearchButton;

    @FindBy(xpath = "//span[@class='x-btn-inner x-btn-inner-center'][contains(@id,'NewAccountButton-btnInnerEl')]")
    public WebElement CreateNewButton;

    @FindBy(css = "input[id='TabBar:AccountTab:AccountTab_AccountNumberSearchItem-inputEl']")
    public WebElement AccountSearch;

    @FindBy(xpath = "//tbody[contains(@id,'gridview')]//tr[1]//td[2]")
    public WebElement SelectAccountNumber;



    public void accountMouseHover(WebDriver driver) {

        WebDriverWait wait1 = new WebDriverWait(driver, 10);

        wait1.until(ExpectedConditions.visibilityOf(accountDropDown));

        Actions builder2 = new Actions(driver);

        builder2.moveToElement(accountDropDown).moveByOffset(50, 0).click()
                .build().perform();

        System.out.println("Dropdown is opened");

    }

    public void accountSearch(String AccountName, WebDriver driver)

    {


        WebDriverWait  wait = new WebDriverWait(driver, 20);

         wait.until(ExpectedConditions .visibilityOf(NewAccount_Button));


        driver.manage().window().maximize();

        Actions builder1 = new Actions(driver);

        builder1.moveToElement(NewAccount_Button).click().build().perform();

        System.out.println("Clicked on New Accounr Button");

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Companyname.sendKeys(AccountName);

        try {

            AccountsearchButton.click();

        } catch (Exception e) {
            System.out
                    .println("Please enter one of the minimum required fields: Company Name, FEIN"
                            + e);
            throw (e);
        }


         wait.until(ExpectedConditions .visibilityOf(CreateNewButton));

        CreateNewButton.click();

    }

  }

测试脚本1: LoginClass:

**

package TestScripts;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;

import Pages.LoginPage;
import Utility.Configreader;
import Utility.GenericMethods;

public class Login {

     WebDriver driver;

    LoginPage Login_page;

    @BeforeSuite
    public void setUp() {

        Configreader cr = new Configreader(
                "H://Selenium//Selenium_ODSRegression//TestData//config.properties");

        driver = GenericMethods.startBrowser("Chrome", cr.getURL());

        Login_page = PageFactory.initElements(driver, LoginPage.class);

    }

    @Test
    public void PClogin() {

        Login_page.pCLogin("su", "gw", driver);

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }

}

**

测试脚本2: AccountCreation类:

package TestScripts;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;

import Pages.AccountFileSummary;
import Pages.Accounttab;
import Pages.CreateNewAccount;

public class NewAccountCreation {

    WebDriver driver;

    Accounttab Account_tab1;

    CreateNewAccount NewAccount1;

    AccountFileSummary AFS1;

    public NewAccountCreation(WebDriver ldriver)

    {
        this.driver = ldriver;

        Account_tab1 = PageFactory.initElements(driver, Accounttab.class);

    }

    @Test
    public void AccountCreation1() {

        Account_tab1.accountMouseHover(driver);

        Account_tab1.accountSearch("WebDriver_Test1", driver);

    }

}

驱动程序类:

package Utility;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;

import Utility.Configreader;

public class GenericMethods {

    public static WebDriver driver = null;

    static Configreader cr = new Configreader(
            "H://Selenium//Selenium_ODSRegression//TestData//config.properties");

    public static WebDriver startBrowser(String browsername, String URL)

    {
        if (browsername.equalsIgnoreCase("Chrome"))

        {
            System.setProperty("webdriver.chrome.driver",
                    cr.getChromeDriverPath());

            ChromeOptions options = new ChromeOptions();
            options.addArguments("test-type");
            options.addArguments("start-maximized");
            options.addArguments("--js-flags=--expose-gc");
            options.addArguments("--enable-precise-memory-info");
            options.addArguments("--disable-popup-blocking");
            options.addArguments("--disable-default-apps");
            options.addArguments("test-type=browser");
            options.addArguments("disable-infobars");

            driver = new ChromeDriver(options);

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        }

        else if (browsername.equalsIgnoreCase("IE"))

        {
            System.setProperty("webdriver.ie.driver", cr.getIEDriverPath());

            driver = new InternetExplorerDriver();

            driver.manage().window().maximize();
        }

        driver.get(URL);

        return driver;

    }

}

属性文件:

PCURL = http://biltipolicycenter.thehartford.com/pc/PolicyCenter.do
ChromeBrowserPath = C://Selenium//ChromeDriver 2.27//chromedriver.exe
IEBrowserPath = C://Selenium//IEDriverServer x86 2.53//IEDriverServer.exe

的testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="TestScripts.Login"/>
      <class name="TestScripts.NewAccountCreation"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

1 个答案:

答案 0 :(得分:0)

NewAccountCreation是一个测试类,因为它包含测试方法,因为您在testng.xml中将其声明为类测试。

所以,这意味着TestNG必须能够创建一个类的实例,这就是TestNG抱怨的原因。

您有两个选择:

  1. 使用a factory向TestNG解释如何创建测试类 (最佳选择IMO)
  2. 使用静态共享驱动程序实例(Login#driver不是 但是静态的)
  3. 顺便说一句,你的班级组织看起来有点脏,你应该稍微改写它们。

相关问题