Serenity跨平台统一解决方案

时间:2017-01-19 14:10:37

标签: java selenium guice-3

我正在尝试使现有解决方案适用于任何平台。制作扩展DriverSource类的自定义驱动程序很容易,但我仍然必须解决不同的页面对象注入(例如在移动设备上运行具有不同的布局)。为此,我想使用Google Guice

我将分享一个没有DI的解决方案,但是应该有更好的方法。

public interface GooglePageInterface {

    public void enter_keywords(String keyword);

    public void lookup_terms();

    public void navigateToHomePage();
}

(您也可以使用抽象类而不是接口,因为大多数方法可能对移动和桌面都是相同的)

接口的两个实现:

 public class GoogleMobilePage extends PageObject implements GooglePageInterface {

        @FindBy(name = "q")
        private WebElementFacade searchTerms;

        @FindBy(id = "tsbb")
        private WebElementFacade lookupButton;

        @Override
        public void enter_keywords(String keyword) {
            element(searchTerms).waitUntilVisible();
            searchTerms.sendKeys(keyword);
        }

        @Override
        public void lookup_terms() {
            lookupButton.click();
        }

        // demo purpose- there are better ways for this
        @Override
        public void navigateToHomePage() {
            getDriver().get("https://www.google.ro/");
        }
    }

 public class GoogleDesktopPage extends PageObject implements GooglePageInterface {

        @FindBy(name = "q")
        private WebElementFacade searchTerms;

        @FindBy(css = "button[name='btnG']")
        private WebElementFacade lookupButton;

        @Override
        public void enter_keywords(String keyword) {
            element(searchTerms).waitUntilVisible();
            searchTerms.sendKeys(keyword);
        }

        @Override
        public void lookup_terms() {
            lookupButton.click();
        }

        @Override
        public void navigateToHomePage() {
            System.out.println("You are in GoogleDesktopPage")
            getDriver().get("https://www.google.ro/");
        }

AbstractSteps将根据Cmd imnplementation

中给出的系统属性来决定
public class AbstractSteps extends ScenarioSteps {

    private static final long serialVersionUID = 1L;

    public GooglePageInterface getDictionaryPage() {

        switch (System.getProperty("runPlatform")) {

        case "desktop":
            return getPages().currentPageAt(GoogleDesktopPage.class);
        case "mobile":
            return getPages().currentPageAt(GoogleMobilePage.class);
        default:
            return null;
        }
    }
}

EndUserStep将扩展AbstractSteps并使用方法

public class EndUserSteps extends AbstractSteps{

    private static final long serialVersionUID = 1L;

    @Step
    public void enters(String keyword) {
        getDictionaryPage().enter_keywords(keyword);
    }

    @Step
    public void starts_search() {
        getDictionaryPage().lookup_terms();
    }

    @Step
    public void navigateToHomePage() {
        getDictionaryPage().navigateToHomePage();
    }

    @Step
    public void looks_for(String term) {
        enters(term);
        starts_search();
    }
}

基本测试:

@RunWith(SerenityRunner.class)
public class SearchByKeywordStory {

    @Managed(uniqueSession = true)
    public WebDriver webdriver;

    @Steps
    public EndUserSteps endUserSteps;

    @Test
    public void searching_by_keyword_apple_should_display_the_corresponding_article() {
        endUserSteps.navigateToHomePage();
        endUserSteps.looks_for("something");
    } 
}

我使用命令运行测试:

mvn test -Dtest = SearchByKeywordStory -Dwebdriver.driver = provided -DrunPlatform = mobile -DrunEnv = staging-env verify, 其中提供了移动设备的自定义驱动程序:

public class CustomDriver implements DriverSource {

        @Override
        public WebDriver newDriver() {
            return setChromeMobile();
        }

        @Override
        public boolean takesScreenshots() {
            return true;
        }

        private WebDriver setChromeMobile() {
            Map<String, Object> deviceMetrics = new HashMap<String, Object>();
            //landscape
            deviceMetrics.put("width", 732);
            deviceMetrics.put("height", 412);
            deviceMetrics.put("pixelRatio", 3.0);
            Map<String, Object> mobileEmulation = new HashMap<String, Object>();
            mobileEmulation.put("deviceMetrics", deviceMetrics);
            mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko)                 Chrome/55.0.2883.87 Mobile Safari/537.36");
            Map<String, Object> chromeOptions = new HashMap<String, Object>();
            chromeOptions.put("mobileEmulation", mobileEmulation);
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
            return new ChromeDriver(capabilities);
        }
    } 

在serenity.properties中

webdriver.driver=provided
webdriver.provided.type = mydriver
webdriver.provided.mydriver = com.tools.customerDrivers.CustomDriver
thucydides.driver.capabilities = mydriver

完成,它有效!

但正如我之前所说,我想尝试使用Guice来完成这项工作,但是我已经遇到了问题,因为Serenity使用Google Guice实现了驱动程序和步骤。

页面界面和页面实现保持不变。

步骤:

  public class EndUserSteps2 extends ScenarioSteps{

        private static final long serialVersionUID = 1L;

        @Inject
        GooglePageInterface  dictionaryPage;

        @Step
        public void enters(String keyword) {
            dictionaryPage.enter_keywords(keyword);
        }

        @Step
        public void starts_search() {
            dictionaryPage.lookup_terms();
        }

        @Step
        public void navigateToHomePage() {
            dictionaryPage.navigateToHomePage();
        }

        @Step
        public void looks_for(String term) {
            enters(term);
            starts_search();
        }

我创建了一个创建injecor的BaseTest(这可以通过不同的方式完成) 完成绑定的模块-bind(GooglePageInterface.class).to(GoogleDesktopPage.class) - 可以在不同的类中

  @RunWith(SerenityRunner.class)
    public class BaseTest {

        protected Injector injector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                bind(GooglePageInterface.class).to(GoogleDesktopPage.class);
            }
        });

        @Before
        public void setup() {
            injector.injectMembers(this);
        }

    }

测试:

@RunWith(SerenityRunner.class)
public class SearchByKeywordStory2 extends BaseTest {

    @Managed(uniqueSession = true)
    public WebDriver webdriver;

    //here is the issue
    @Inject
    @Steps
    public EndUserSteps2 endUserSteps;

    @Test
    public void searching_by_keyword_apple_should_display_the_corresponding_article() {
        endUserSteps.navigateToHomePage();
        endUserSteps.looks_for("something");
    }
}

测试开始但是驱动程序没有传递给GoogleDesktopPage.class,因此测试失败,但是该类被注入为消息&#34;您在GoogleDesktopPage&#34; from navigateToHomePage()打印出来

我知道Serenity注入了webdriver以及使用注释的步骤,我有点卡在这里

如果有人能帮助我以某种方式完成这项工作,我真的很感激(可能是与DI不同的方法,因为这似乎不是一个好的方法)

0 个答案:

没有答案