如何在 Selenium Webdriver 中关闭两个浏览器实例?

问题描述 投票:0回答:1
        public void ECM_20245() throws Exception {
        String org = null;
        checkout = new CommonCheckoutModule();
        mini = new MiniCartModule();
        orderAction = new OrderActionsModule();
        ShoppingCartModule cart = new ShoppingCartModule();
        orderHist = new IntelligoStoreOrderHistoryLandingModule();
        order = new OrderHistoryLandingModule();
        org = ConfigurationLibrary.BuyingForData.get("Buy_InvoiceStore");
        OrgSelectorModule select = new OrgSelectorModule();
        String roleA = ConfigurationLibrary.prop.getProperty("ECCROLE_ADMIN_STORE");
        OrderConfirmationModule confirm = new OrderConfirmationModule();
        checkout.buyingForMyself();
        checkout.checkoutSingleIndividualCertificationPrdOrg();
        mini.validateMiniCart().viewCartClick();
        cart.validateShoppingCartPage();
        cart.getGrandTotalPriceFromCart();
        cart.ClickProductFromShoppingCart();
        mini.clearCart();
        select.orgBuyingFor(org);
        checkout.SingleIndividualCertificationoption();
        mini.validateMiniCart().viewCartClick();
        cart.validateShoppingCartPage();
        cart.checkoutSelection();
        confirm.chooseOrgFromConfirmation(org).selectConfirmation();
        checkout.enterPurchaseCodeForStore().selectInvoiceAndSubmit();
        checkout.orderSuccessValidation();
        String orderId = order.getOrderIDFromOrderConfirmationPage();
        orderHist.ctaLinkValOrderActivationPageForIC();


        CommonLibrary.newBrowserLaunch();
        ConfigurationLibrary.loginForAllEnv(roleA, "UserId1");
        orderAction.orderSearchInAdmin(orderId).storeOrderDetailsValForOrg();
        
    }

上面代码中CommonLibrary.newBrowserLaunch();启动新浏览器并执行一组代码。Testng aftermethods 关闭最新打开的浏览器,但不关闭现有浏览器。我无法使用 driver.quit 因为 Testng 执行将并行.. 两个浏览器实例都可以关闭吗? (可以用@aftermethods关闭)

selenium-webdriver automation automated-tests testng
1个回答
0
投票

设置 windowHandles = driver.getWindowHandles();

// Iterate through the window handles and close each window
for (String windowHandle : windowHandles) {
    driver.switchTo().window(windowHandle);
    driver.close();
}

您可以尝试上面的代码来关闭所有打开的窗口

© www.soinside.com 2019 - 2024. All rights reserved.