如何在testng

时间:2017-01-27 16:40:57

标签: java selenium testng

    import org.testng.annotations.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class redc {
    public static WebDriver driver=null;
    public static  void startdriver()
    {
    String key="webdriver.gecko.driver";
    String path="C:\\selenium3\\geckodriver\\geckodriver.exe";
    System.setProperty(key,path);
    driver = new FirefoxDriver();
        }
    @BeforeMethod
    public void call()
    {


    }
    @Test
    public void t1()
    {
        driver.get("https://www.dynamiclevels.com/");
    }

我正在尝试运行此代码,但不断收到错误

  

FAILED:t1 java.lang.NullPointerException at   datac.redc.t1(redc.java:25)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)

请帮我纠正代码。如果可能的话,以一种可行的方式重写它。我确信我犯了同样的愚蠢错误,我无法抓住。 :(

1 个答案:

答案 0 :(得分:1)

@BeforeTest
public void setup() {
    String key= "webdriver.gecko.driver";
    String path= "C:\\selenium3\\geckodriver\\geckodriver.exe";
    System.setProperty(key, path);
    driver = new FirefoxDriver();
}

并从WebDriver中删除静态:

private WebDriver driver;