简体   繁体   English

当使用TestnG运行Selenium时,我得到FAILED CONFIGURATION:BeforeClass setUp?

[英]When Running Selenium with TestnG I get FAILED CONFIGURATION: BeforeClass setUp?

@BeforeClass I get null null errors I believe it's something with my before class.What about optional do you need them? @BeforeClass我得到null null错误我相信这是我课前的事情。关于可选的你需要它们吗? FAILED CONFIGURATION: @BeforeClass setUp(null, null) I tried to add different maven dependiencies maybe its because of selenium version its still wont work. FAILED CONFIGURATION:@BeforeClass setUp(null,null)我试图添加不同的maven依赖,可能因为它的selenium版本仍然无法正常工作。

    public class Practice {

     WebDriver driver;
    //Check this ont out
   @BeforeClass(alwaysRun = true)
    @Parameters({ "browser", "url" })
    public void setUp(@Optional("browser") String browser, @Optional("url") String url)  {
        BaseTest base = new BaseTest(browser, url);
        driver = base.getDriver();

    }

@Test
public void Check() {
    try {
        System.out.println("Passed Test case...");

        Assert.assertTrue(driver.getTitle().contentEquals("Google"));

        } catch (Exception ee) {
        System.out.println("NOOOOO ");
        ee.printStackTrace();

        Assert.assertEquals("noooo " + ee, "iT SHOULD NOT fAIL");
    }

} }

public class Selen {

private WebDriver driver;
private String browser;
private String url;


public Selen(String browser, String url) {
    this.browser = browser;
    this.url = url;

    if (browser.equalsIgnoreCase("firefox")) {

        System.setProperty("webdriver.firefox.marionette",
                "C:\\Users\\geckodriver.exe");

        final FirefoxProfile firefox = new FirefoxProfile();


        driver = new FirefoxDriver();

        driver.get(url);

    }

    else if (browser.equalsIgnoreCase("chrome")) {
        // set path to chromedriver.exe
        System.setProperty("webdriver.chrome.driver", "C:\\Drivers\\chromedriver.exe");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--enable-automation", "test-type=browser", "--disable-plugins", "--disable-infobars",
                "--disable-notifications", "start-maximized");
        driver = new ChromeDriver(options);
        driver.get(url);

    }

    else if (browser.equalsIgnoreCase("Edge")) {
        // set path to Edge.exe
        System.setProperty("webdriver.edge.driver", ".\\MicrosoftWebDriver.exe");

        driver = new EdgeDriver();
        driver.get(url);
    } else {

    }


}

public String getBrowser() {
    return this.browser;
}

public String getBaseUrl() {
    return this.url;
}

public WebDriver getDriver() {
    return this.driver;
}

@AfterClass
public void tearDown(WebDriver driver) {
    quitDriver(driver);
}

protected static void quitDriver(WebDriver driver) {
    try {
        if (driver != null) {
            driver.quit();
        }
    } catch (Exception ee) {
        System.out.println("Failed: " + ee);
        ;
    }

} }

**Failed Configuration**

[RemoteTestNG] detected TestNG version 6.14.3
Check() test case...
There was a problem: 
java.lang.NullPointerException
    at com.seleniumae.exercise.Practice.Check(Practice.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)

TeNg.xml TeNg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
  <parameter name="browser" value= "Chrome" />
   <parameter name="url" value="https://www.google.com/"/>
    <classes>
      <class name="com.seleniumae.exercise.Practice"/>
      <class name="com.seleniumae.exercise.Practice1"/>
      <class name="com.seleniumae.exercise.Practice2.java"/>
    </classes>
  </test> <!-- Test -->
  <test thread-count="5" name="Test">
  <parameter name="browser" value ="Firefox" />
   <parameter name="url" value="https://www.google.com/"/>
    <classes>
     <class name="com.seleniumae.exercise.Practice"/>
      <class name="com.seleniumae.exercise.Practice1"/>
      <class name="com.seleniumae.exercise.Practice2"/>
    </classes>
  </test> <!-- Test -->
  <test thread-count="5" name="Test">
  <parameter name="browser" value="InternetExplore" />
  <parameter name="url" value="https://www.google.com/"/>
    <classes>
     <class name="com.seleniumae.exercise.Practice"/>
      <class name="com.seleniumae.exercise.Practice1"/>
      <class name="com.seleniumae.exercise.Practice2.java"/>
    </classes>
  </test> <!-- Test -->

</suite> <!-- Suite -->

The issue could be about parameters with browser taking one parameter like chrome when it should be another parameter. 问题可能是关于参数与浏览器采取像chrome这样的参数应该是另一个参数。

Syntax to define @Optional annotation : 定义@Optional注释的语法:

public void setUp(@Optional("browser_name") String browser, @Optional("site_name") String url) public void setUp(@Optional(“browser_name”)字符串浏览器,@ Option(“site_name”)字符串url)

And you are implementing in following way without using @Optional("value") 并且您正在以下列方式实现而不使用@Optional(“value”)

public void setUp(@Optional String browser, @Optional String url) throws MalformedURLException { public void setUp(@Optional String browser,@ Optional url)抛出MalformedURLException {

TestNG.xml - please write parameters like below TestNG.xml - 请写下面的参数

<suite name="Suite"> 
<test thread-count="5" name="Test"> 
<parameter name="url" value="https://www.google.com/"/> 
<parameter name="browser" value= "Chrome" />

Note - TestNG provides you flexibility of declaring parameters as optional. 注 - TestNG为您提供了将参数声明为可选的灵活性。 When we declare a parameter as optional and If defined parameter is not found in your testng.xml file, The test method will receive the default value which is specified inside the @Optional annotation. 当我们将参数声明为可选参数并且如果在testng.xml文件中找不到已定义的参数时,测试方法将接收在@Optional注释中指定的默认值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 配置失败:@BeforeMethod 设置 - Selenium Java TestNG - FAILED CONFIGURATION: @BeforeMethod setUp - Selenium Java TestNG TestNg 在组中运行时跳过 @BeforeSuite 和 @BeforeClass - TestNg skipping @BeforeSuite and @BeforeClass when running in group 配置失败:@BeforeClass设置失败,配置失败:@AfterClass撕裂java.lang.NullPointerException错误 - Having FAILED CONFIGURATION: @BeforeClass setUp and FAILED CONFIGURATION: @AfterClass tearDown java.lang.NullPointerException errors 失败的配置:@BeforeTest beforeTest 。 行家; 测试NG,硒 - FAILED CONFIGURATION: @BeforeTest beforeTest . MAVEN; TestNG, Selenium 运行两次Testng的beforeClass测试方法 - beforeClass test method running twice Testng TestNG @BeforeClass初始化代码未在Test之前运行 - TestNG @BeforeClass initialization code not running before Test 失败的配置:@BeforeTest beforeTest将此代码作为testNg运行 - FAILED CONFIGURATION: @BeforeTest beforeTest on running this code as testNg 失败的配置:@BeforeClass setUp org.mockito.exceptions.base.MockitoException:无法初始化@Spy注释字段&#39;allRequestParams&#39; - FAILED CONFIGURATION: @BeforeClass setUp org.mockito.exceptions.base.MockitoException: Unable to initialize @Spy annotated field 'allRequestParams' TestNG失败时重新运行测试,而不运行BeforeClass AfterClass方法 - TestNG Re run test on failure, not running BeforeClass AfterClass methods TestNG:@BeforeClass 方法失败时会跳过所有后续的测试类吗? - TestNG: All subsequent Test classes are skipped when @BeforeClass method fails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM