Java 未打开第二个要素文件的新浏览器窗口

Java 未打开第二个要素文件的新浏览器窗口,java,selenium,cucumber,Java,Selenium,Cucumber,当我运行TestRunner类时,浏览器在第一个特性之后关闭,而在第二个特性启动时不打开新浏览器。 获取以下错误消息 构建信息:版本:“3.7.0”,修订版:“2321c73”,时间:“2017-11-02T22:22:35.584Z” 系统信息:主机:'FIDJ0MXS2Y',ip:'10.76.49.102',os.name:'Windows 10',os.arch:'amd64',os.version:'10.0',java.version:'1.8.0221' 驱动程序信息:org.op

当我运行TestRunner类时,浏览器在第一个特性之后关闭,而在第二个特性启动时不打开新浏览器。 获取以下错误消息

构建信息:版本:“3.7.0”,修订版:“2321c73”,时间:“2017-11-02T22:22:35.584Z” 系统信息:主机:'FIDJ0MXS2Y',ip:'10.76.49.102',os.name:'Windows 10',os.arch:'amd64',os.version:'10.0',java.version:'1.8.0221' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能{AcceptSecureCerts:false,browserName:chrome,browserVersion:75.0.3770.80,chrome:{chromedriverVersion:75.0.3770.140(2d9f97485c7b…,userDataDir:C:\Users\sdad\AppData\Local…),goog:chromeOptions:{debuggerAddress:localhost:16773},javascriptEnabled:true,networkConnectionEnabled:false,pageLoadStrategy:normal,platform:XP,platformName:XP,proxy:proxy(),setWindowRect:true,StrictFileInteractibility:false,超时:{隐式:0,pageLoad:300000,脚本:30000},未经处理的PromptBehavior:Discover and notify} 会话ID:071afb952f9a259f9df29a9809d3e4b7

package context;

import java.util.HashMap;
import java.util.Map;

public class ScenarioContext
{
    private Map<String, Object> scenarioContext;

    public ScenarioContext(){
        scenarioContext = new HashMap<>();
    }

    public void setContext(String key, Object value){
        scenarioContext.put(key.toString(), value);
    }

    public Object getContext(String key){
        return scenarioContext.get(key);
    }

    public Boolean isContains(String key){
        return scenarioContext.containsKey(key);
    }
}

这就是使用可以提供帮助的地方。这将允许您为每个场景创建新的web驱动程序保险,并将其传递给所有步骤定义类,而不必依赖静态。正如您所发现的,在使用Selenium和cucumber时,静态不是您的朋友。这是使用可以提供帮助的地方。这将允许您为每个场景创建新的web驱动程序保险,并将其传递给所有步骤定义类,而不必依赖静态。正如您所发现的,使用Selenium和cucumber时,静态不是您的朋友。
package context;

import managers.PageObjectManager;
import managers.WebDriverManager;

public class TestContext
{
    private WebDriverManager webDriverManager;
    private PageObjectManager pageObjectManager;
    private ScenarioContext scenarioContext;

    public TestContext()
    {
        webDriverManager = new WebDriverManager();
        pageObjectManager = new PageObjectManager(webDriverManager.getWebDriver());
        scenarioContext = new ScenarioContext();
    }
    public WebDriverManager getWebDriverManager()
    {
        return webDriverManager;
    }

    public PageObjectManager getPageObjectManager()
    {
        return pageObjectManager;
    }

    public ScenarioContext getScenarioContext(){
        return scenarioContext;
    }
}
package managers;

import org.openqa.selenium.WebDriver;
import pages.*;

public class PageObjectManager
{
    private WebDriver driver;

    HomePage homePage;
    Page1 page1;
    Page2 page2;

    public PageObjectManager(WebDriver driver)
    {
        this.driver = driver;
    }

    public HomePage getHomePage()
    {
        return (homePage == null) ? homePage = new HomePage(driver) : homePage;
    }

    public Page1 getPage1()
    {
        return (page1 == null) ? page1 = new Page1(driver) : page1;
    }

    public Page2 getPage2()
    {
        return (page2 == null) ? page2 = new Page2(driver) : page2;
    }

}
package managers;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebDriverManager
{
    private static WebDriver driver;

    public WebDriverManager(){}

    synchronized public static WebDriver getWebDriver()
    {
        if(driver == null)
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\sdad\\Downloads\\Softwares\\BrowserDrivers\\chromedriver.exe");
            driver = new ChromeDriver();
            return driver;
        }
        else
        {
            return driver;
        }
    }
}
package pages;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;

public class HomePage {

    WebDriver driver;
    public HomePage(WebDriver driver)
    {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void goToHomePage()
    {
        this.driver.navigate().to("C:\\Users\\sdad\\Downloads\\Projects\\Learning\\WebPages\\page_1.html");
    }

    public void closeBrowser()
    {
        this.driver.close();
//        this.driver.quit();
    }
}
package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class Page1
{
    WebDriver driver;
    @FindBy(name = "fname_page_1")
    WebElement firstName;

    @FindBy(name = "lname_page_1")
    WebElement lastName;

    @FindBy(xpath = "//a[text()='Page 2']")
    WebElement page2Button;

    public Page1(WebDriver driver)
    {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public void enterFirstName(String firstName)
    {
        this.firstName.sendKeys(firstName);
    }
    public void enterLastName(String lastName)
    {
        this.lastName.sendKeys(lastName);
    }
    public void clickPage2Button(){
        page2Button.click();
    }
}
package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class Page2
{
    WebDriver driver;
    @FindBy(name = "fname_page_2")
    WebElement firstName;

    @FindBy(name = "lname_page_2")
    WebElement lastName;

    @FindBy(xpath = "//a[text()='Page 3']")
    WebElement page3Button;

    public Page2(WebDriver driver)
    {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
    public void enterFirstName(String firstName)
    {
        this.firstName.sendKeys(firstName);
    }
    public void enterLastName(String lastName)
    {
        this.lastName.sendKeys(lastName);
    }
    public void clickPage3Button()
    {
        page3Button.click();
    }
}
Feature: Page 1 Feature

  Scenario: Page 1 - Scenario - 1
    Given I will in home page

  Scenario: Page 1 - Scenario - 2
    Then I will enter Dada in page 1 first name
    And I will enter peer in page 1 last name
    And I will click page 2 button
    And I will close the browser
Feature: Page 2 Feature

  Scenario: Page 2 - Scenario - 1
    Given I will in home page

  Scenario: Page 2 - Scenario - 2
    And I will click page 2 button
    Then I will enter Dada in page 2 first name
    And I will enter peer in page 2 last name
    And I will click page 3 button
    And I will close the browser
package stepDefinitions;

import context.TestContext;
import cucumber.api.java.en.And;
import pages.HomePage;

public class HomePageStep {

    TestContext testContext;
    HomePage homePage;

    public HomePageStep(TestContext testContext)
    {
        this.testContext = testContext;
        homePage = testContext.getPageObjectManager().getHomePage();
    }

    @And("^I will in home page$")
    public void goToHomePage()
    {
        homePage.goToHomePage();
    }

    @And("^I will close the browser$")
    public void closeBrowser()
    {
        homePage.closeBrowser();
    }

}
package stepDefinitions;

import context.TestContext;
import cucumber.api.java.en.And;
import pages.Page1;

public class Page1Step {
    TestContext testContext;
    Page1 page1;

    public  Page1Step(TestContext testContext)
    {
        this.testContext = testContext;
        page1 = testContext.getPageObjectManager().getPage1();
    }

    @And("^I will enter ([^\"]*) in page 1 first name$")
    public void enterFirstName(String firstName)
    {
        page1.enterFirstName(firstName);
    }

    @And("^I will enter ([^\"]*) in page 1 last name$")
    public void enterLastName(String lastName)
    {
        page1.enterLastName(lastName);
    }

    @And("^I will click page 2 button$")
    public void clickPage2Button() throws InterruptedException {
        page1.clickPage2Button();
        Thread.sleep(3000);
    }
}
package stepDefinitions;

import context.TestContext;
import cucumber.api.java.en.And;
import pages.Page2;

public class Page2Step {
    TestContext testContext;
    Page2 page2;

    public  Page2Step(TestContext testContext)
    {
        this.testContext = testContext;
        page2 = testContext.getPageObjectManager().getPage2();
    }

    @And("^I will enter ([^\"]*) in page 2 first name$")
    public void enterFirstName(String firstName)
    {
        page2.enterFirstName(firstName);
    }

    @And("^I will enter ([^\"]*) in page 2 last name$")
    public void enterLastName(String lastName)
    {
        page2.enterLastName(lastName);
    }

    @And("^I will click page 3 button$")
    public void clickPage3Button() throws InterruptedException {
        page2.clickPage3Button();
        Thread.sleep(3000);
    }
}