首页 > 解决方案 > 在同一场景中在移动和桌面浏览器/驱动程序之间切换 - java selenium cucumber

问题描述

基本前提是我想在同一场景中在默认(桌面)和移动仿真之间来回切换(你称之为不同的驱动程序吗?或不同的选项卡/窗口?)。

任何导航到后端的东西都将始终使用默认驱动程序。如果 maven 命令行中存在移动标签,前端导航将使用移动仿真@mobile(已经让此代码工作)。

默认驱动程序已在现有抽象类中设置,我知道如何设置移动仿真,但我不确定如何在两者之间“切换”。

下面是我想要完成的一个例子:

-------------------------------
DEFAULT NAVIGATION FEATURE FILE
-------------------------------
@navigation
# no @mobile tag so both steps use default driver
Feature: Navigate to back and front ends
    Scenario: Navigate to back end then front end
    When I navigate to the back end (default driver)
    When I navigate to the front end (default driver)

------------------------------
MOBILE NAVIGATION FEATURE FILE
------------------------------
@navigation
# @mobile tag so want to toggle between both drivers
@mobile
Feature: Navigate to back and front ends
    Scenario: Navigate to back end then front end
    When I navigate to the back end (default driver)
    When I navigate to the front end (mobile emulation)

------------
DRIVER CLASS
------------
    public class GetWebDriver() {
        public GetWebDriver() {

        }

        public static WebDriver driver(boolean mobile) {
            WebDriver driver;
            if (mobile) {
                // close desktop if it's open???
                driver = mobile emulation;
            } else {
                // close mobile if it's open???
                driver = default;
            }
        }
        return driver;
    }


--------------
NAVIGATE CLASS
--------------

    import static com.postmedia.automation.browser.AbstractPage.driver; // this is currently how we're setting up driver in our classes
    public class NavigateToSite() {
        public NavigateToSite() {

        }

        public void navigateFrontEnd(boolean mobile) {              
            GetWebDriver.driver(mobile).get("frontEndUrl"); // mobile or desktop
        }

        public void navigateBackEnd() {
            GetWebDriver.driver(false).get("backEndUrl"); // always use default
        }
    }

标签: javaselenium

解决方案


推荐阅读