JBehave Serenity:如何管理baseURL和相对URL?

时间:2018-10-15 06:31:30

标签: java maven-3 jbehave serenity-bdd

我是Serenity的新手,我了解到我们可以从命令行更改哪个默认URL。

这就是我声明默认网址的方式

@DefaultUrl("http://en.wiktionary.org/wiki/Wiktionary")
public class DictionaryPage extends PageObject {


}

但是对于每个页面,如果我继续声明这样的URL,那么我将失去关于声明默认URL的概念。

我要寻找的只是在某处指定默认URL并提供如下相对页面URL:

@DefaultUrl($baseURL+"/wiki/Wiktionary")
public class DictionaryPage extends PageObject {


}

我该如何实现?

serenity.properties 如下所示:

# Define the default driver
#webdriver.driver=phantomjs
# Appears at the top of the reports
serenity.project.name = Demo Project using Serenity and JBehave
serenity.restart.browser.for.each = NEVER
# Root package for any JUnit acceptance tests
#serenity.test.root=net.thucydides.showcase.junit.features
# Customise your requirements hierarchy
#serenity.requirement.types=feature, story
# Run the tests without calling webdriver - useful to check your JBehave wireing
#serenity.dry.run=true
# Customise browser size
#serenity.browser.height = 1200
#serenity.browser.width = 1200

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.testing.browser</groupId>
    <artifactId>browser-tests</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Sample Serenity project using JBehave and WebDriver</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <serenity.version>1.8.3</serenity.version>
        <serenity.jbehave.version>1.34.0</serenity.jbehave.version>
        <webdriver.driver>firefox</webdriver.driver>
    </properties>

    <repositories>
      <repository>
        <snapshots>
        <enabled>false</enabled>
        </snapshots>
        <id>central</id>
        <name>bintray</name>
        <url>http://jcenter.bintray.com</url>
      </repository>
    </repositories>
    <pluginRepositories>
      <pluginRepository>
        <snapshots>
        <enabled>false</enabled>
        </snapshots>
        <id>central</id>
        <name>bintray-plugins</name>
        <url>http://jcenter.bintray.com</url>
      </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-jbehave</artifactId>
            <version>${serenity.jbehave.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                        <include>**/*TestSuite.java</include>
                        <include>**/Test*.java</include>
                        <include>**/When*.java</include>
                    </includes>
                    <argLine>-Xmx512m</argLine>
                    <systemPropertyVariables>
                        <webdriver.driver>${webdriver.driver}</webdriver.driver>
                    </systemPropertyVariables>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2 个答案:

答案 0 :(得分:1)

在serenity.properties文件中输入以下属性

webdriver.base.url = http://en.wiktionary.org

在页面对象中,像这样定义DefaultUrl

@DefaultUrl("/wiki/Wiktionary")
public class DictionaryPage extends PageObject {


}

Serenity将在调用DefaultUrl时构造完整的URL。

有关serenity.properties中所有可用属性的更多参考,请参考下面的链接 https://github.com/serenity-bdd/serenity-documentation/blob/master/src/asciidoc/system-props.adoc

答案 1 :(得分:0)

除此之外,我认为您可以在页面对象中使用别名,如下所示:

@DefaultUrl("/wiki/Wiktionary")
@NamedUrls({
    @NamedUrl(name = "glossary", url = "/wiki/Appendix:Glossary"),
    @NamedUrl(name = "community_portal", url = "/wiki/Wiktionary:Community_Portal")
})
public class DictionaryPage extends PageObject {}

并在您的步骤中使用它们:

public class EndUserSteps {

    DictionaryPage dictionaryPage;

    @Step
    public void navigatesUsingDeepLink() {
        dictionaryPage.open("productId_1");
    }
}