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

我尝试了几个选项,但仍然出现此错误:org.testng.testngexception:检查以确保它可以实例化

程墨竹
2023-03-14

我是selenium和自动化测试的初学者,我正在使用java、TestNG和Maven编写selenium脚本。当我在一个类中编写所有内容时,所有的工作都很好,但我希望有一个用于所有对象的包,一个用于测试的包,以及带有main设置的基类。该项目将包含来自网站的每个页面的类和所有测试的类。当我试图修改一些东西时,出现了另一个错误,这是来自BaseClass中的构造函数,我不知道为什么基类是这样的:我用

public BaseClass(WebDriver driver) {           
    this.driver = driver; 
    PageFactory.initElements(driver, this);
}

pagegeFactory.initElements(new AjaxElementLocatorFactory(driver,TimeoutValue),this);并且不起作用。

package com.selenium.Base;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import page_objects.AnalizesiPreturi;

import java.util.concurrent.TimeUnit;

public class BaseClass  {


    public WebDriver driver;
    protected static AnalizesiPreturi baseClass;

    public BaseClass(WebDriver driver) {  }

    public BaseClass() {    }

    @BeforeMethod
    public void setUp() {

        System.setProperty("webdriver.driver.chrome", "C://SeleniumWebdrivers//chromedriver.exe");
        driver = new ChromeDriver();
        //implicit wait
        driver.manage().window().maximize();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.MINUTES);

        //launch URL
        driver.get("http://googl.com/");

        //scroll
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollBy(0,500)", "");

        //WebDriverWailt for all objects located by class name
        WebDriverWait wbd = new WebDriverWait(driver, 1000);
        wbd.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("AnalizesiPreturi")));
    }
    @AfterMethod
    public void tearDown(){
        driver.close();
    }
}

Page objects类是否来自另一个包

package page_objects;
import com.gargoylesoftware.htmlunit.Page;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import com.selenium.Base.BaseClass;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

public class AnalizesiPreturi extends BaseClass {
    public AnalizesiPreturi(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    @FindBy(css = "#onetrust-accept-btn-handler")
    WebElement cookie;
    @FindBy(css = "#footable_501 > tbody > tr.ninja_table_row_0.nt_row_id_77 > td.ninja_column_1.ninja_clmn_nm_pret.footable-last-visible")
    WebElement plus1Button;
    @FindBy(css = "#footable_501 > tbody > tr.ninja_table_row_1.nt_row_id_78 > td.ninja_column_1.ninja_clmn_nm_pret.footable-last-visible")
    WebElement plus2Button;
    @FindBy(css = "#calculator > div.body > div.products > div.innerProducts > div > div.right")
    WebElement minusButton;
    @FindBy(css = "#show-hide > div.hide")
    WebElement closeButton;
    @FindBy(css = "#footable_501 > thead > tr.footable-filtering > th > form > div > div > input")
    WebElement search;
    @FindBy(css = "#footable_501 > thead > tr.footable-filtering > th > form > div > div > div > button.btn.btn-primary")
    WebElement findButton;

    @Test
    public AnalizesiPreturi addAnalises(){
        PageFactory.initElements(driver, this);
        cookie.click();
        plus1Button.click();
        plus2Button.click();
        closeButton.click();
        return null;
    }
}

最后一个是测试包

package Test;
import com.selenium.Base.BaseClass;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import page_objects.AnalizesiPreturi;
import page_objects.Centers;

public class TestsPage extends BaseClass {
    public TestsPage(WebDriver driver) {
        super(driver);
    }
    public WebDriver getDriverInstance() {
        return driver;
    }

    @Test
    public void TestAnalises()throws Exception{
        //Tests
        driver.get("");
        AnalizesiPreturi analise = new AnalizesiPreturi(driver);
        analise.addAnalises();
    }
    public void TestCenters(){
        Centers centre = new Centers(driver);
        centre.checkCenters();
    }
}

共有1个答案

司迪
2023-03-14

您的代码的基本问题是您已经参数化了您的构造函数。

TestNG只知道如何调用默认构造函数(无参数构造函数)。在您的例子中,您有一个参数化的构造函数,它似乎接受WebDriver实例,而TestNG不知道如何实例化它。

解决此问题的最简单方法是执行以下操作:

    null

更多详情请参阅我的博客文章https://rationaleemotions.com/paralle_webdriver_executions_using_testng/

 类似资料:
  • 我知道这是个老问题,但它真的把我困住了。 我采纳了使用classLoader获取inputsream的建议,代码如下: 我确认ResourceUtil.GetInputStream工作正常,因为我可以打印hibernate.cfg.xml的内容,但为什么仍然给我错误: 信息:HHH000040:配置资源:/hibernate.cfg.xml线程“main”org.hibernate.hiberna

  • 找不到方法“”无效组织。阿帕奇。平民压紧档案管理员。拉链齐普菲尔。(java.nio.channels.seekablebytechnel)“”无效组织。阿帕奇。平民压紧档案管理员。拉链齐普菲尔。(java.nio.channels.seekablebytechnel)' Gradle的依赖缓存可能损坏(有时会在网络连接超时后发生)。 重新下载依赖项和同步项目(需要网络)Gradle构建进程(守护

  • 我收到了一个名为 我在这里做错了什么?我无法修复这个错误,我正在尝试使机器人检查我的不和ID,然后发送我的“注释”在那里的屏幕截图。 下面是我的mongodb指南针的截图。 https://imgur.com/a/r0pl106 这就是我的职能。

  • 有人能解释一下为什么在我的例子中,用组件替换RestController注释没有任何明显的效果吗?我的控制器: 这以同样的方式工作:

  • 我有一个angular 2应用程序,它使用firebase进行身份验证。我想使用谷歌作为我的身份验证提供者,并有一切设置,以便它应该工作。 如果我尝试用signinWithPopup进行身份验证(如文档中提到的),它可以工作: 但是,如果使用重定向尝试相同的代码,则会出现错误: 有人知道为什么弹出式方法有效而重定向方法无效吗?

  • 我正在从SDK版本30移动到31,并在意向过滤器中添加了android:exported,但仍然出现以下错误: 合并错误:Error:android:exported需要为显式指定。当相应组件定义了意图过滤器时,针对Android 12及更高版本的应用程序需要为指定一个显式值。看见https://developer.android.com/guide/topics/manifest/activit