当前位置: 首页 > 知识库问答 >
问题:

执行testng时出错。使用pagefactory的xml

夹谷衡
2023-03-14

第一个类运行成功,但第二个类失败与错误:

“org.testng.TestNGException:无法调用public void TestScripts.NewAccountCreation.AccountCreation1():将其设为静态或向类中添加无参数构造函数”

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

我使用PageFactory来设计我的测试用例。

Eclipse版本:开普勒

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();

    }

}

Page账户创建页面的工厂代码:

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:登录类:

**

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个答案

卢景澄
2023-03-14

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

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

您有两种选择:

  1. 使用工厂向TestNG解释如何创建测试类(IMO的最佳选项)

顺便说一句,您的类组织看起来有点脏,您应该对它们进行一些修改。

 类似资料:
  • 我有一个应用程序(使用注释的Spring 4 MVC Hibernate 4 MySQL Maven集成示例),使用基于注释的配置将Spring与Hibernate集成,但运行测试时出错! 上课时间到了 下面是即将到来的考验: 这是pom。xml文件:

  • 我想用pytesseract。这是我的密码。 结果我得到了这个错误 回溯(最后一次调用):文件“C:\Users\user\AppData\Local\Programs\Python 38-32\lib\site packages\pdf2image\pdf2image.py”,第409行,在pdfinfo\u from_path proc=Popen(命令,env=env,stdout=PIPE

  • 我找不到解决方法,所以我在这里发帖寻求帮助。我在并行运行测试时遇到问题(使用POM和page factory)。 *\Testbase类*/ *POM类* *测试用例* *XML文件* 我收到java nullpointerexception错误 **更新我碰到的错误,/*POM类*\createnewpage方法驱动程序返回一个空参数。 无论如何,我在这里尝试的是运行两个chrome浏览器进行并

  • 我刚遇到这个错误,它突然来了,破坏了我的项目。有没有人可以帮助我解决这个错误:

  • 突然我开始得到这个错误,我不明白为什么如果有人让我知道这个错误在哪里,会有足够的帮助。因为android Studio的新更新,我能得到的就是这个。我得到的错误的详细摘要。

  • 执行 时出错。 这是错误日志: 11-05 05:03:20.171:E/Android runtime(3171):致命异常:async task # 1 < br > 11-05 05:03:20.171:E/Android runtime(3171):Process:com . example . MySQL test,PID:3171 < br > 11-05 05:03:20.171:E