使用 selenium Webdriver 自动化基于 Web 的应用程序时如何停止自动重定向 URL

问题描述 投票:0回答:0

我正在尝试导航到 URL:https://www.lambdatest.com/ 但系统自动重定向到 URL:https://www.lambdatest.com/pricing 页面,因此我的测试用例失败。

甚至尝试过隐身模式,但发生了重定向。

请尽快帮助解决问题。

并且无法与selenium gird连接。

请参考以下代码。

公共类测试场景{

WebDriver driver;
JavascriptExecutor js;
String currentWindow;

@BeforeClass
@Parameters({ "browser", "browserVersion", "platform" })
public void setUp(String browser, String browserVersion, String platform) {

    String username = "XXXXXX";
    String accessKey = "XXXXXX";
    /*
     * HashMap<String, Object> ltOptions = new HashMap<String, Object>(); //
     * ltOptions.put("username", "XXXXXX"); 
     * ltOptions.put("accessKey",
     * "XXXXX");
     * ltOptions.put("visual", true); ltOptions.put("video", true);
     * ltOptions.put("network", true); ltOptions.put("build", "Build 0.1");
     * ltOptions.put("project", "Selenium Advanced"); ltOptions.put("name",
     * "Retest"); ltOptions.put("tunnel", true); ltOptions.put("w3c", true);
     * ltOptions.put("plugin", "java-testNG");
     * 
     * if (browser.equalsIgnoreCase("chrome")) { ChromeOptions browserOptions = new
     * ChromeOptions();
     * 
     * browserOptions.setPlatformName(platform);
     * browserOptions.setBrowserVersion(browserVersion);
     * browserOptions.setCapability("LT:Options", ltOptions);
     * 
     * try { driver = new RemoteWebDriver( new URL("https://" + username + ":" +
     * accessKey + "@hub.lambdatest.com/wd/hub"), browserOptions);
     * 
     * } catch (MalformedURLException e) { // TODO Auto-generated catch block
     * e.printStackTrace(); } driver.manage().window().maximize();
     * driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
     * 
     * } else if (browser.equalsIgnoreCase("MicrosoftEdge")) {
     * 
     * EdgeOptions browserOptions = new EdgeOptions();
     * 
     * browserOptions.setPlatformName(platform);
     * browserOptions.setBrowserVersion(browserVersion);
     * browserOptions.setCapability("LT:Options", ltOptions);
     * 
     * try { driver = new RemoteWebDriver( new URL("https://" + username + ":" +
     * accessKey + "@hub.lambdatest.com/wd/hub"), browserOptions);
     * 
     * } catch (MalformedURLException e) { // TODO Auto-generated catch block
     * e.printStackTrace(); } driver.manage().window().maximize();
     * driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
     * 
     * } else { throw new IllegalArgumentException("Invalid browser name: " +
     * browser); }
     */

    if (browser.equalsIgnoreCase("chrome")) {

        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

    } else if (browser.equalsIgnoreCase("MicrosoftEdge")) {

        driver = new EdgeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

    }

}

@Test(priority = 1)
public void testcase1() {
    /*
     * 1. Navigate to https://www.lambdatest.com/.
     */

    driver.get("https://www.lambdatest.com/");

    /*
     * 2. Perform an explicit wait till the time all the elements in the DOM are
     * available. 3. Scroll to the WebElement ‘SEE ALL INTEGRATIONS’ using the
     * scrollIntoView() method. You are free to use any of the available web
     * locators (e.g., XPath, CssSelector, etc.)
     */

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));

    wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*")));

    WebElement seeAllIntegrations = driver.findElement(By.linkText("SEE ALL INTEGRATIONS"));
    js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView(true);", seeAllIntegrations);

    /*
     * 4. Click on the link and ensure that it opens in a new Tab.
     * 
     * 5. Save the window handles in a List (or array). Print the window handles of
     * the opened windows (now there are two windows open).
     */

    currentWindow = driver.getWindowHandle();

    Actions ac = new Actions(driver);
    ac.moveToElement(seeAllIntegrations).contextClick().build().perform();
    ac.sendKeys(Keys.DOWN, Keys.ENTER).build().perform();

    Set<String> allOpenedWindows = driver.getWindowHandles();

    wait.until(ExpectedConditions.numberOfWindowsToBe(2));

    List<String> handles = new ArrayList<String>(allOpenedWindows);

    for (String child : handles) {
        if (!currentWindow.equals(child))
            driver.switchTo().window(child);

    }

    /*
     * 6. Verify whether the URL is the same as the expected URL (if not, throw an
     * Assert).
     */

    String expectedUrl = "https://www.lambdatest.com/integrations";
    Assert.assertEquals(driver.getCurrentUrl(), expectedUrl, "URL is not the same as expected.");

}

@Test(priority = 2)
public void testcase2() {
    /*
     * 7. On that page, scroll to the page where the WebElement (Codeless
     * Automation) is present.
     */

    WebElement codelessAutomation = driver
            .findElement(By.xpath("//*[@id=\"__next\"]/div[1]/section[2]/div/div/div[1]/div/ul/li[5]/a"));

    codelessAutomation.click();

js.executeScript("arguments[0].scrollIntoView(true);", codelessAutomation);

    /*
     * 8. Click the ‘LEARN MORE’ link for Testing Whiz. The page should open in the
     * same window.
     */

    WebElement learnMore = driver.findElement(By.xpath("//*[@id=\"codeless_row\"]/div/div[4]/a"));
    learnMore.click();
    /*
     * 9. Check if the title of the page is ‘TestingWhiz Integration |
     * LambdaTest’.If not, raise an Assert.
     */

    String expectedTitle = "TestingWhiz Integration | LambdaTest";
    Assert.assertEquals(driver.getTitle(), expectedTitle, "Title is not as expected.");

    /*
     * 10. Close the current window using the window handle [which we obtained in
     * step (5)]
     */
    driver.close();
    driver.switchTo().window(currentWindow);

    int currentWindowCount = driver.getWindowHandles().size();
    System.out.println("Current window count: " + currentWindowCount);

}

@Test(priority = 3)
public void testCase3() {

    /*
     * 12. On the current window, set the URL to https://www.lambdatest.com/blog.
     * 13. Click on the ‘Community’ link and verify whether the URL is
     * https://community.lambdatest.com/. 14. Close the current browser window.
     */

    driver.get("https://www.lambdatest.com/blog");

    driver.findElement(By.xpath("//*[@id=\"menu-item-10121\"]/a")).click();

    String expectedUrl = "https://community.lambdatest.com/";

    Assert.assertEquals(driver.getCurrentUrl(), expectedUrl, "URL is not the same as expected.");

    driver.close();
}

@AfterClass
public void tearDown() {
    driver.quit();
}

}

XML 文件:-

“https://testng.org/testng-1.0.dtd”>

  • 如果尝试连接下面的网格会抛出错误

    [RemoteTestNG] 检测到 TestNG 版本 7.8.0 SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默认为无操作(NOP)记录器实现 SLF4J:请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder 了解更多详细信息。 2023 年 7 月 30 日下午 3:07:39 org.openqa.selenium.remote.tracing.opentelemetry.OpenTelemetryTracer createTracer 信息:使用 OpenTelemetry 进行跟踪

    ================================================== Lambda测试套件 运行测试总数:6,通过:0,失败:0,跳过:6 配置失败:2,跳过:2

selenium-webdriver selenium-chromedriver selenium-ide selenium-grid
© www.soinside.com 2019 - 2024. All rights reserved.