gpt4 book ai didi

java - 使用 testng 运行 selenium 代码时收到 NullPointerException 错误消息

转载 作者:行者123 更新时间:2023-12-02 09:40:14 25 4
gpt4 key购买 nike

package utils;

import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

import java.util.concurrent.TimeUnit;

public class BrowserUtils {

public WebDriver driver = null;

@BeforeTest
public void setUp(){
System.setProperty("webdriver.chrome.driver","C:\\Users\\sdad\\Downloads\\Softwares\\BrowserDrivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().setPosition(new Point(0, 0));
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
}

@AfterTest
public void tearDown(){
driver.close();
driver.quit();
}
}
package parallel_3;

import org.testng.Assert;
import org.testng.annotations.Test;
import parallel_2.Headers;
import parallel_2.Images;
import parallel_2.Styles;
import utils.BrowserUtils;

public class Test_01 extends BrowserUtils {
Headers headers;
Images images;
Styles styles;

private final String HEADER_FILE_PATH ="C:\\Users\\sdad\\Downloads\\Projects\\Demo-Website\\Headers.html",
IMAGES_FILE_PATH ="C:\\Users\\sdad\\Downloads\\Projects\\Demo-Website\\Images.html";

@Test
public void test(){
driver.get(HEADER_FILE_PATH);
headers = new Headers(driver);
Assert.assertEquals(driver.getTitle(), "Headers");
Assert.assertEquals(headers.header4.getText(), "This is Header 4");
headers.images_link.click();

images = new Images(driver);
Assert.assertEquals(driver.getTitle(), "Images");
Assert.assertEquals(images.header2.getText(), "Image with width and height");
images.styles_link.click();

styles = new Styles(driver);
Assert.assertEquals(driver.getTitle(), "Page Styles");
Assert.assertEquals(styles.paragraph.getText(), "This is a paragraph");
styles.images_link.click();
}
}
package parallel_3;

import org.testng.Assert;
import org.testng.annotations.Test;
import parallel_2.Headers;
import parallel_2.Images;
import parallel_2.Styles;
import utils.BrowserUtils;

public class Test_02 extends BrowserUtils {
Headers headers;
Images images;
Styles styles;

private final String HEADER_FILE_PATH ="C:\\Users\\sdad\\Downloads\\Projects\\Demo-Website\\Headers.html",
IMAGES_FILE_PATH ="C:\\Users\\sdad\\Downloads\\Projects\\Demo-Website\\Images.html";

@Test
public void test(){
driver.get(IMAGES_FILE_PATH);

images = new Images(driver);
Assert.assertEquals(driver.getTitle(), "Images");
Assert.assertEquals(images.header2.getText(), "Image with width and height");
images.styles_link.click();

styles = new Styles(driver);
Assert.assertEquals(driver.getTitle(), "Page Styles");
Assert.assertEquals(styles.paragraph.getText(), "This is a paragraph");
styles.images_link.click();

headers = new Headers(driver);
Assert.assertEquals(driver.getTitle(), "Headers");
Assert.assertEquals(headers.header4.getText(), "This is Header 4");
headers.images_link.click();
}
}

错误信息

java.lang.NullPointerException
at parallel_3.Test_01.test(Test_01.java:20)
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)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

最佳答案

@BeforeTest 不会针对所有 @Test 运行,仅在第一个 @Test 之前运行,因此 driver 永远不会初始化。来自 testng docs

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

您可以使用@BeforeMethod注释来实现

@BeforeMethod: The annotated method will be run before each test method.

对于@AfterTest@AfterMethod也是如此。

关于java - 使用 testng 运行 selenium 代码时收到 NullPointerException 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57142846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com