简体   繁体   English

如何在 Chrome 浏览器上启动 Serenity Cucumber BDD?

[英]How to launch Serenity Cucumber BDD on chrome browser?

I'm new to Serenity BDD and not sure why my test is running always on Firefox with the code I have attached.我是 Serenity BDD 的新手,不知道为什么我的测试总是在 Firefox 上使用我附加的代码运行。 Adding a web driver variable annotated with @Managed(driver="chrome") isn't making any difference.添加用 @Managed(driver="chrome") 注释的 Web 驱动程序变量没有任何区别。 Is there a way I can direct the framework to launch the chrome browser using "serenity.properties"?有没有办法可以指导框架使用“serenity.properties”启动 chrome 浏览器?

GoogleSearchTest.feature谷歌搜索测试功能

Feature: Test google search
      As a user I want to
      search google
  Scenario: Test google search box
    Given the google page is loaded
    When user search for "Gmail"
    Then gmail results should be displayed

DefinitionSteps.java定义步骤.java

package com.testserenity.steps;

import cucumber.api.PendingException;
import net.thucydides.core.annotations.Managed;
import net.thucydides.core.annotations.Steps;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import com.testserenity.steps.serenity.EndUserSteps;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

import java.util.List;

public class DefinitionSteps {

    @Steps
    EndUserSteps user;

    @Managed(driver = "chrome")
    WebDriver driver;

    @Given("^the google page is loaded$")
    public void theGooglePageIsLoaded() throws Throwable {

       user.opensUpTheAUT("https://www.google.com");
    }
    @Then("^gmail results should be displayed$")
    public void gmailResultsShouldBeDisplayed() throws Throwable {

        user.shouldBeAbleTOFindAllLinksOf("Gmail");

    }

    @When("^user search for \"([^\"]*)\"$")
    public void userSearchFor(String str) throws Throwable {
        user.searchesOnGoogle(str);
        user.hitsKey(Keys.ENTER);
        Thread.sleep(2000);
    }
}

EndUserSteps.java最终用户步骤.java

package com.testserenity.steps.serenity;

import com.testserenity.pages.CommonActions;
import com.testserenity.pages.GooglePage;
import net.thucydides.core.annotations.Step;
import org.openqa.selenium.Keys;
import org.testng.Assert;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;

public class EndUserSteps {

    GooglePage googlePage;
    CommonActions commonActions;


    @Step
    public void searchesOnGoogle(String keyword) {

       googlePage.sendText(keyword);

    }
    @Step    
    public void shouldBeAbleTOFindAllLinksOf(String gmail) {

        Assert.assertNotEquals(googlePage.totalNumberOfLinksWithString("Gmail"),0);


    }
    @Step
    public void opensUpTheAUT(String s) {

        googlePage.open();

    }

    @Step
    public void hitsKey(Keys enter) {

        googlePage.keyboardActions(enter);
    }


   }

Common Actions.java通用操作.java

package com.testserenity.pages;

import com.google.common.base.Predicate;
import net.thucydides.core.pages.PageObject;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;

/**
 * Created by User on 10/18/2018.
 */
public class CommonActions extends PageObject {

    public CommonActions(WebDriver driver) {
        super(driver);
    }


    public void keyboardActions(Keys enter) {

    }
}

GooglePage.java谷歌页面

package com.testserenity.pages;

import net.serenitybdd.core.pages.PageObjects;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.pages.PageObject;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;

/**
 * Created by User on 10/12/2018.
 */
@DefaultUrl("https://www.google.com")
public class GooglePage extends CommonActions{

    public GooglePage(WebDriver driver) {
        super(driver);
    }

    @FindBy(name = "q")
    public WebElement SearchBox;



    @FindBy(xpath = "//*[contains(text(),'Gmail')]")
    public List<WebElement> GmailLinkList;

    public void sendText(String gmail) {

        SearchBox.sendKeys(gmail);

    }

    public Integer totalNumberOfLinksWithString(String s) {
        try {
            Thread.sleep(3000);
        }catch (Exception e){
            e.getStackTrace();
        }
        return this.GmailLinkList.size();
    }

    @Override
    public void keyboardActions(Keys enter) {
        super.keyboardActions(enter);
        this.SearchBox.sendKeys(enter);
    }
}

pom.xml 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.testserenity</groupId>
    <artifactId>TestSerenityBdd</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <serenity.version>1.8.3</serenity.version>
        <serenity.cucumber.version>1.6.6</serenity.cucumber.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>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.14.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>

        <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-cucumber</artifactId>
            <version>${serenity.cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.7</version>
            <scope>test</scope>
        </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>**/*.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>

You are setting firefox in your pom properties:您正在 pom 属性中设置 firefox:

<webdriver.driver>firefox</webdriver.driver>

Either change this to 'chrome' or remove the webdriver option from the failsafeplugin systemPropertyVariables and handle it through the Managed annotation in code.将其更改为 'chrome' 或从 failsafeplugin systemPropertyVariables 中删除 webdriver 选项,并通过代码中的 Managed 注释进行处理。

<systemPropertyVariables>
    <webdriver.driver>${webdriver.driver}</webdriver.driver>
</systemPropertyVariables>

You need to remove firefox from POM.xml.您需要从 POM.xml 中删除 firefox。

you can use ChromeOptions options = new ChromeOptions();您可以使用 ChromeOptions options = new ChromeOptions();

options.addArguments("--window-size=1920,1080"); options.addArguments("--window-size=1920,1080");

options.addArguments("--window-position=0,0"); options.addArguments("--window-position=0,0");

WebDriver driver = new ChromeDriver(options); WebDriver driver = new ChromeDriver(options);

You can use chromeoptions to set different properties of chrome.您可以使用 chromeoptions 来设置 chrome 的不同属性。

#In your serenity.conf# #在你的宁静.conf#

webdriver {
  driver = chrome
}

OR或者

#In your Serenity.properties:# #在您的 Serenity.properties 中:#

webdriver.base.url = "YOUR_URL"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM