Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Serenity跨平台统一解决方案_Java_Selenium_Guice 3 - Fatal编程技术网

Java Serenity跨平台统一解决方案

Java Serenity跨平台统一解决方案,java,selenium,guice-3,Java,Selenium,Guice 3,我试图使现有的解决方案在任何平台上都能工作。制作一个扩展DriverSource类的自定义驱动程序很容易,但我仍然需要解决不同的页面对象注入(例如,在移动设备上运行的页面对象具有不同的布局)。为此,我想使用谷歌Guice 我将分享一个没有DI的解决方案,它很好用,但应该有更好的方法 public interface GooglePageInterface { public void enter_keywords(String keyword); public void loo

我试图使现有的解决方案在任何平台上都能工作。制作一个扩展DriverSource类的自定义驱动程序很容易,但我仍然需要解决不同的页面对象注入(例如,在移动设备上运行的页面对象具有不同的布局)。为此,我想使用谷歌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/");
        }
    }

抽象步骤将根据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测试-Dtest=SearchByKeywordStory-Dwebdriver.driver=provided-DrunPlatform=mobile-DrunenNV=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);
        }
    } 
完成了,成功了

但正如我之前所说的,我想尝试使用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();
        }
我已经创建了一个BaseTest,其中创建了injecor(这可以通过不同的方式完成) 完成绑定的模块-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,因此测试失败,但当navigateToHomePage()中的消息“You is in GoogleDesktopPage”打印出来时,会插入该类

我知道Serenity使用注释注入了webdriver和步骤,我有点被困在这里了

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

  @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");
    }
}