第二功能文件的新浏览器窗口未打开

时间:2019-10-17 08:36:50

标签: java selenium cucumber

当我运行TestRunner类时,浏览器在第一个功能启动后关闭,而在第二个功能启动时未打开新的浏览器。 收到以下错误消息

内部版本信息:版本:'3.7.0',修订版本:'2321c73',时间:'2017-11-02T22:22:35.584Z' 系统信息:主机:'FIDJ0MXS2Y',ip:'10 .76.49.102',操作系统名称:'Windows 10',os.arch:'amd64',os.version:'10 .0',java.version:'1.8.0_221 ' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能{acceptInsecureCerts:false,browserName:chrome,browserVersion:75.0.3770.80,chrome:{chromedriverVersion:75.0.3770.140(2d9f97485c7b ...,userDataDir:C:\ Users \ sdad \ AppData \ Local ...},goog:chromeOptions :{debuggerAddress:本地主机:16773},javascriptEnabled:true,networkConnectionEnabled:false,pageLoadStrategy:正常,平台:XP,platformName:XP,代理服务器:Proxy(),setWindowRect:true,strictFileInteractability:false,超时:{隐式:0, pageLoad:300000,脚本:30000},unhandledPromptBehavior:关闭并通知} 会话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);
    }
}
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);
    }
}

Folder Structure

0 个答案:

没有答案