簡體   English   中英

Selenium TestNG框架無法在多個測試中調用驅動程序對象

[英]Selenium TestNG framework unable to call driver object in multiple test

我正在嘗試執行以下程序,但出現空指針錯誤。 @test無法訪問對象-驅動程序。 我可能正在犯什么錯誤。 錯誤-

JavaScript警告: https//www.google.com/xjs/_/js/k=xjs.s.en_US.MHBUsB8Me90.O/m=sx,c,sb,cdos,cr,elog,hsm,jsa,r ,qsm,d,csi / am = wCJGjhccAPk_IRQStxAWyAImDiA / rt = j / d = 1 / t = zcms / rs = ACT90oGwMPBWhsFBKoM1svJAFUVoVQRQug ,第7行:非常慢地改變對象的[[Prototype]];將導致您的代碼運行緩慢; 而是使用Object.create https://www.google.com/?gws_rd=ssl失敗,使用正確的初始[[Prototype]]值創建對象:網站標題

java.lang.NullPointerException
    at myPackage.TestNGforHDFC.websiteTitle(TestNGforHDFC.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)

package myPackage;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNGforHDFC {
    public WebDriver driver;
    public String urlUnderTest = "http://www.google.com";
    public String projLocation = "C:\\Users\\Nikita Agrawal\\Selenium\\geckodriver.exe";
    @BeforeTest
    public void login()
    {
        System.setProperty("webdriver.gecko.driver", projLocation);
        WebDriver driver = new FirefoxDriver();
        driver.get(urlUnderTest);
        System.out.println(driver.getCurrentUrl());
    }

    @Test
public void websiteTitle()
    {
        System.out.println(driver.getTitle());
    }
}

類變量driver不會被初始化,您要在login方法中定義一個稱為driver的新變量。

采用:

public void login(){
    driver = new FirefoxDriver();
    ....
}

代替:

public void login(){
    WebDriver driver = new FirefoxDriver();
    ....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM