Web-Treiber und Fluent Wait ist zwischen den Methoden in TestNG null

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Web-Treiber und Fluent Wait ist zwischen den Methoden in TestNG null

by Anonymous » 27 Jan 2025, 09:26

Ich bin relativ neu im Selenium-Webtreiber und bei TestNG. Ich programmiere ein Projekt, in dem zwei Java-Klassen gleichzeitig ausgeführt werden. Das Problem besteht darin, dass der für jede dieser Klassen eingerichtete Webtreiber und die flüssige Wartefunktion nicht bestehen bleiben lebendig zwischen @Test-Methoden. Dies führt zu diesem Fehler:
java.lang.NullPointerException: „org.openqa.selenium.WebDriver.navigate()“ kann nicht aufgerufen werden, da der Rückgabewert von „java.lang.ThreadLocal.get ()" ist null
Weiß jemand, warum das passieren könnte?

Code: Select all

public class baseTest {

protected static ThreadLocal driver = new ThreadLocal();
protected static ThreadLocal wait = new ThreadLocal();
protected SoftAssert softAssert;

@BeforeSuite
public void setUpSuite() {
System.out.println("Executing method - setUpSuite\n\n");
}

@AfterSuite
public void tearDownSuite() {
System.out.println("Executing method - tearDownSuite\n\n");
// Cleanup
if (driver.get() != null) {
driver.get().quit();
driver.remove();
}
if (wait.get() != null) {
wait.remove();
}
}

@BeforeClass
public void setUpClass() {
System.out.println("Executing method - setUpClass\n\n");

configProp config = new configProp();
driver.set(config.initializeDriver());
wait.set(config.initializeFluentWait(driver.get())); // Initialize FluentWait before the class

// Perform login
config.login(driver.get());
System.out.println("Performed login\n\n");

// Call the testPostmanAuth class
System.out.println("Calling PostmanAuth\n\n");

TestPostmanAuth postmanAuth = new TestPostmanAuth();
try {
postmanAuth.testAuthorizationLoginOnPostman();
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}

@AfterClass
public void tearDownClass() {
System.out.println("Executing method - tearDownClass\n\n");

// Cleanup for the current class
if (driver.get() != null) {
driver.get().quit();
driver.remove();
}
if (wait.get() != null) {
wait.remove();
}
}

@BeforeMethod
public void beforeMethod() {
System.out.println("Executing method - beforeMethod\n\n");
softAssert = new SoftAssert(); // Initialise SoftAssert before each test method

// Check if driver and wait are initialised
if (driver.get() == null || wait.get() == null) {
throw new RuntimeException("WebDriver or FluentWait instance is not initialized.");
}
}

@AfterMethod
public void afterMethod() {
System.out.println("Executing method - afterMethod\n\n");
softAssert.assertAll(); // Ensure assertAll is called after each test method

}

// Getters for driver and wait
public WebDriver getDriver() {
return driver.get();
}

public FluentWait getWait() {
return wait.get();
}
}
Der Treiber und das fließende Warten werden in einer anderen Datei initialisiert:

Code: Select all

public class configProp {

public WebDriver initializeDriver() {
System.setProperty("webdriver.gecko.driver",
"URLGoesHere");

FirefoxOptions options = new FirefoxOptions();
options.setCapability("webSocketUrl", true); // Enable WebDriver BiDi

WebDriver driver = new FirefoxDriver(options);
driver.manage().window().maximize();
return driver;
}

public FluentWait initializeFluentWait(WebDriver driver) {
// Initialise FluentWait
FluentWait wait = new FluentWait(driver).withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofMillis(500)).ignoring(NoSuchElementException.class);
return wait;
}
< /code>
Dann gibt es zwei weitere Dateien, in denen die baSetest -Klasse erweitert wird.  Vollständiger Fehler: bestimmte Teile für den Beitrag entfernt: < /p>
checkIncident
java.lang.RuntimeException: WebDriver or FluentWait instance is not initialized.
at Automation.baseTest.beforeMethod(baseTest.java:81)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
at java.base/java.lang.Thread.run(Thread.java:1575)
... Removed 15 stack frames
(Checks incident number sent matches on website)
Methodenaufruf .navigate:

Code: Select all

    public void switchToIframe() {
WebElement iframe = wait.get().until(ExpectedConditions.visibilityOfElementLocated(By.tagName("iframe")));
driver.get().switchTo().frame(iframe);
}

@Test(priority = 3, dependsOnMethods = {"nextRequestAssignNArrive" }, description = "Refreshes the page so processing can be done")
public void refresh() throws InterruptedException {
System.out.println("\n\nRefreshing the page after some time...");

// Adding a sleep for 30 seconds before refreshing to make sure processing has
// complete and incident is In Progress
Thread.sleep(30 * 1000);

// Refresh the page
driver.get().navigate().refresh();

System.out.println("Page refreshed...");

Thread.sleep(10 * 1000); // just waiting for refresh to be done

// make sure to switch to iframe after refresh
switchToIframe();
}

Top