我对Selenium Web驱动程序和Testng的新事物相对较新,我正在编码一个项目,其中两个Java类同时运行。问题是,对于每个类@Test方法。导致此错误:

问题描述 投票:0回答:1
public class baseTest { protected static ThreadLocal<WebDriver> driver = new ThreadLocal<>(); protected static ThreadLocal<FluentWait<WebDriver>> 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<WebDriver> getWait() { return wait.get(); } }

在另一个文件中启动了驱动程序和流利的等待:

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<WebDriver> initializeFluentWait(WebDriver driver) { // Initialise FluentWait FluentWait<WebDriver> wait = new FluentWait<>(driver).withTimeout(Duration.ofSeconds(20)) .pollingEvery(Duration.ofMillis(500)).ignoring(NoSuchElementException.class); return wait; }

然后还有其他两个文件,其中最高的类别是扩展的 这两个文件是@test方法所在的位置。 Fulll错误:删除了某些零件以供发布:
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) 

Method致电.navigate:
    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();
        }

来自

priority = 3, dependsOnMethods = {"nextRequestAssignNArrive" },
您的测试应不会出错。我已经修改了您的刷新测试以反映建议的更改:

refresh()

我看不到根据优先级进行测试的原因。尽管如此,您的测试还是按照安排的方式进行的,例如堆栈;首先执行顶级测试,其余的则遵循。
    
java selenium-webdriver testng
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.