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
Selenium 在所有的场景之前,先考虑_Selenium_Jvm_Cucumber - Fatal编程技术网

Selenium 在所有的场景之前,先考虑

Selenium 在所有的场景之前,先考虑,selenium,jvm,cucumber,Selenium,Jvm,Cucumber,在jvm中,如果我们有多个场景,并且有任何选项可以在一个实例中执行所有场景。我的意思是打开浏览器一次并执行所有场景,而不是为每个场景打开和关闭浏览器 我在hooks文件中尝试了BeforeAll,但它抛出了null指针异常 这是我的密码。我需要转到URL,使用凭据登录,并在同一个实例上执行这两个场景 马哈拉特特特酒店 Feature: MaharaTest Scenario: Creating Profile Given I navigate to Content Tab A

在jvm中,如果我们有多个场景,并且有任何选项可以在一个实例中执行所有场景。我的意思是打开浏览器一次并执行所有场景,而不是为每个场景打开和关闭浏览器

我在hooks文件中尝试了BeforeAll,但它抛出了null指针异常

这是我的密码。我需要转到URL,使用凭据登录,并在同一个实例上执行这两个场景

马哈拉特特特酒店

Feature: MaharaTest
  Scenario: Creating Profile
    Given I navigate to Content Tab
    And I enter firstname
    And I enter lastname
    And I click save profile
    Then I see profile saved successful message
  Scenario: Creating Profile
    Given I navigate to Portfolio Tab
    And I click create page
    And I enter page title
    And I click save page
    Then I see page saved successful message
MaharaTest.java

public class MaharaTest {
    public WebDriver driver;
    public MaharaTest() {
        driver = Hooks.driver;
    }

    @Given("^I navigate to Content Tab$")
    public void navigate_to_content() {
        driver.findElement(By.linkText("Content")).click();
    }

    @And("^I enter firstname$")
    public void enter_firstname() {
        driver.findElement(By.id("profileform_firstname")).clear();
        driver.findElement(By.id("profileform_firstname")).sendKeys("Test");
    }

    @And("^I enter lastname$")
    public void enter_lastname() {
        driver.findElement(By.id("profileform_lastname")).clear();
        driver.findElement(By.id("profileform_lastname")).sendKeys("User");
    }

    @And("^I click save profile$")
    public void click_save() {
        driver.findElement(By.id("profileform_submit")).click();
    }

    @Then("^I see profile saved successful message$")
    public void profile_success_message() {
        assertEquals("Profile saved successfully",
                driver.findElement(By.className("alert-success")).getText());
    }

    @Given("^I navigate to Portfolio Tab$")
    public void navigate_to_portfolio() {
        driver.findElement(By.linkText("Portfolio")).click();
    }

    @And("^I click create page$")
    public void create_page() {
        driver.findElement(By.id("createview_submit")).click();
    }

    @And("^I enter page title$")
    public void enter_page_title() {
        driver.findElement(By.id("editview_title")).clear();
        driver.findElement(By.id("editview_title")).sendKeys("Test_Profile");
    }

    @And("^I click save page$")
    public void click_save_page() {
        driver.findElement(By.id("editview_submit")).click();
    }

    @Then("^I see page saved successful message$")
    public void page_success_message() {
        assertEquals("Page saved successfully",
                driver.findElement(By.className("alert-success")).getText());
    }
}
Hooks.java

public class Hooks {
    public static WebDriver driver;
    @Before
    public void openBrowser() throws MalformedURLException {
        System.out.println("Called openBrowser");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://demo.mahara.org/");
        driver.findElement(By.id("login_login_username")).sendKeys("student1");
        driver.findElement(By.id("login_login_password")).sendKeys("Testing1");
        driver.findElement(By.id("login_submit")).click();
    }

    @After
    public void embedScreenshot(Scenario scenario) {

        if (scenario.isFailed()) {
            try {
                scenario.write("Current Page URL is " + driver.getCurrentUrl());
                byte[] screenshot = ((TakesScreenshot) driver)
                        .getScreenshotAs(OutputType.BYTES);
                scenario.embed(screenshot, "image/png");
            } catch (WebDriverException somePlatformsDontSupportScreenshots) {
                System.err.println(somePlatformsDontSupportScreenshots
                        .getMessage());
            }

        }
        driver.quit();
    }
}

首先,你似乎想在这里做两件事

创建配置文件 创建公文包页面

我假设你不能创建一个公文包页面,除非你已经创建了一个配置文件

一种方法是:

Scenario: Create a profile
  When I create a profile
  Then I should see my new profile
这应该足以推动创建概要文件的发展。一旦您可以创建一个配置文件,那么您就可以

Scenario: Create portfolio page
  Given I have a profile
  When I create a portfolio page
  Then I should see my new portfolio page
如果我有一个配置文件,有两种方法可以实现

  • 执行与创建配置文件时使用的代码完全相同的代码,即通过UI

  • 找到一种更快的方法来创建相同的效果(通常绕过UI)

  • 如果您想要一组快速的功能,您将采用第二种方法

    需要注意的一些事项:

    无需在场景中详细说明“如何”创建配置文件/页面。场景应该是关于什么和为什么不关于如何,所以不要点击按钮或填写字段(将其留给步骤defs)


    因为我们写得很快(因为我有一个配置文件),所以我们可以在很多场景中使用它,而不用担心需要多长时间。

    感谢您的输入。但是,如果我需要执行第二个场景,即创建公文包页面,它将只正确启动一个新窗口,而不是在为第一个场景启动的相同浏览器上工作,如何克服这一问题?