Selenium WebDriver java.lang.NullPointerException
我过去曾使用过这种方法,但最近我对selenium做得不多。我正在尝试设置此测试,以使多个案例按顺序运行 这是我的密码Selenium WebDriver java.lang.NullPointerException,java,selenium,selenium-webdriver,junit,Java,Selenium,Selenium Webdriver,Junit,我过去曾使用过这种方法,但最近我对selenium做得不多。我正在尝试设置此测试,以使多个案例按顺序运行 这是我的密码 import org.junit.*; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.o
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import java.time.Duration;
public class TotalAutomatation {
static WebDriver driver;
@BeforeClass public static void setup() throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\con15096\\snow_demo\\autotile\\drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://nk.okta.com/login/login.htmfromURI=%2Fapp%2Fservicenow_app2%2Fexkbcm77encfzK3a80x7%2Fsso%2Fsaml%3FSAMLRequest%3DnVJdb5swFP0ryO9gMGkhVojEEk2L1nWoyfqwl8mBS2vF2MzX0Ky%252FfsTJ1O5hVbVH2%252Beej3u8QNEp1vNycI%252F6Dn4OgC44dkojP78UZLCaG4ESuRYdIHc135ZfbjiLYt5b40xtFAlKRLBOGr0yGocO7BbsKGv4dndTkEfneuSUYi%252BtdEJaJTVgA2OEZ1SozVNUm45qMfbiAaLGkGA9eZFanEhfKPQhMgcnPFj0Pb0QTPM%252FpiOjcDzs6y7LQNft8%252BdU5PExo4iGnuKQ4KOxNfiwBWmFQiDBZl2Q7e2qnWe1SK%252FZ1fwaGtam6TxncTtvr6DJ9yzJJiBWAlGO8DKKOMBGoxPaFYTFSR7GecjSXRzz2YzPkihm6XcSVJc1fZC6kfrh7Z3uzyDkn3a7Kqy%252BbneeYJQN2NsJ%252Fb%252FrvAeLfpWTCFkufL3cJ7CvG3%252FbnPhTM1m%252B28WCvta6KPf8lGWzroyS9a%252BgVMo8rSwIN%252BVzdgBfVSfcv%252B0kUeJvZBO2HsqhE1KVTWMBkdDlRffvz738DQ%253D%253D%26RelayState%3Dhttps%253A%252F%252Fspiritairlinesdev.service-now.com%252Fsolveit%253Fid%253Dsolveit_dept_dev");
Thread.sleep(1000);
driver.findElement(By.id("okta-signin-username")).sendKeys("username");
Thread.sleep(1500);
driver.findElement(By.id("okta-signin-password")).sendKeys("password");
Thread.sleep(1000);
driver.findElement(By.id("okta-signin-submit")).click();
Thread.sleep(1000);
driver.get("https://spiritairlinesdev.service-now.com/solveit");
}
@Test public void getFirstTile1() throws InterruptedException {
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id=\"xd150fd31cb10020000f8d856634c9ce6\"]/li[1]/a")).click();
Thread.sleep(6000);
Select dropList = new Select(driver.findElement(By.id("edit_tile_level")));
dropList.selectByVisibleText("FlightOPS/ Inflight");
Thread.sleep(1500);
dropList.selectByVisibleText("Submit a New Request");
Thread.sleep(1500);
dropList.selectByVisibleText("System Access Request");
Thread.sleep(2000);
WebElement TemplateCard = driver.findElement(By.xpath("//*[@id=\"template_color\"]"));
WebElement card1= driver.findElement(By.xpath("//*[@id=\"02844eecdb486700433c38ff9d961914_color\"]"));
new Actions(driver)
.moveToElement(card1)
.pause(Duration.ofSeconds(1))
.clickAndHold(card1)
.pause(Duration.ofSeconds(1))
.moveToElement(TemplateCard)
.pause(Duration.ofSeconds(1))
.release().perform();
Thread.sleep(2000);
System.out.println("I have the first tile");
driver.findElement(By.id("previous_parent")).click();
Thread.sleep(1500);
driver.findElement(By.id("previous_parent")).click();
Thread.sleep(1500);
driver.findElement(By.id("previous_parent")).click();
Thread.sleep(1500);
dropList.selectByVisibleText("Airport Stations");
Thread.sleep(1500);
dropList.selectByVisibleText("Submit a New Request");
Thread.sleep(2000);
dropList.selectByVisibleText("System Access Request");
Thread.sleep(2000);
driver.findElement(By.id("save_new_tile")).click();
}
@AfterClass public static void closeWindow() throws InterruptedException {
Thread.sleep(1500);
driver.close();
}
}
它运行@BeforeTest用例,但在尝试进入第一个@Test时失败
我很难理解为什么在@BeforeClass之后它会中断。
WebDriver driver = new FirefoxDriver();
您可以创建一个局部变量方法。所以静态字段永远不会被写入
只需删除
WebDriver
就可以真正访问静态字段。哪一行抛出nullreferenceexception?全局、本地、运行时、编译时、初始化、声明都写得正确-1促使我写出非常有限的答案
public class TotalAutomatation {
// This is global variable
// this variable is declared but not initialized
static WebDriver driver;
@BeforeClass
// I am happy at compile time and run time as I can
// access local variable which is declared and initialized both
public static void setup() throws InterruptedException {
// this is local variable with same name
// this variable is declared and initialized also
// I have overridden driver value for BeforeClass
WebDriver driver = new FirefoxDriver();
} // scope ended here for local variable
@Test
// I am not happy at run time as I have access to global variable
// which is only declared but not initialized
public void getFirstTile1() throws InterruptedException {
// here at run time global variable will be accessed which
// is static variable which and it is null :(
// My next statement will suffer NullPointerException :-(
driver.findElement();
}