需要为多个链接传递参数以在黄瓜中进行测试

Need to pass parameter for multiple links to test in cucumber

我写了这个功能文件来测试家里的多个链接 page.I 试图通过功能文件传递参数来减少步骤定义的数量。 我在特征文件中写入元素名称时遇到问题,它可以获取每个 tab.There 没有提到 id 所以我不得不采用 xpath(我知道在特征文件中提到它并不理想”。你能建议任何替代方法吗到了吗?

Homepage.feature

  Scenario: To Test Home Tab
      Given I am on Homepage
      When I Click on ".//*[@id='oneHeader']/div[3]/div/div[2]/nav/ul/li[1]/a/span"
      Then I am on "Home"
      And application should be closed

  Scenario: To Test Calender Tab
    Given I am on Homepage
    When I Click on "Calender"
    Then I am on "Calender"
    And application should be closed

  Scenario: To Test Lead Tab
    Given I am on Homepage
    When I Click on "Leads"
    Then I am on "Leads"
    And application should be closed

  Scenario: To Test Oppurtunities Tab
    Given I am on Homepage
    When I Click on "Oppurtunities"
    Then I am on "Oppurtunities"
    And application should be closed

  Scenario: To Test Accounts Tab
    Given I am on Homepage
    When I Click on "Accounts"
    Then I am on "Accounts"
    And application should be closed


  Scenario: To Test Contacts Tab
    Given I am on Homepage
    When I Click on "Contacts"
    Then I am on "Contacts"
    And application should be closed

  Scenario: To Test Dashboard Tab
    Given I am on Homepage
    When I Click on "Dashboards"
    Then I am on "Dashboards"
    And application should be closed

  Scenario: To Test Reports Tab
    Given I am on Homepage
    When I Click on "Reports"
    Then I am on "Reports"
    And application should be closed

步骤定义如下所示

package stepDefination;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SmokeTest {
    WebDriver driver;

    public SmokeTest() {
    }

    @Given("^Open firefox and start application$")
    public void Open_chrome_and_start_application() throws Throwable {
        this.driver = new FirefoxDriver();
        this.driver.manage().window().maximize();
        this.driver.get("http://test.salesforce.com/");
    }

    @When("^I enter valid \"([^\"]*)\" and valid \"([^\"]*)\"$")
    public void I_enter_valid_and_valid(String unam, String pass) throws Throwable {
        this.driver.findElement(By.xpath(".//*[@id=\'username\']")).sendKeys(new CharSequence[]{unam});
        this.driver.findElement(By.xpath(".//*[@id=\'password\']")).sendKeys(new CharSequence[]{pass});
    }

    @Then("^I should be able to login successfully$")
    public void user_should_be_able_to_login_successfully() throws Throwable {
        this.driver.findElement(By.id("Login")).click();
    }

    @Given("^I am on Homepage$")
    public void i_am_on_Homepage() throws Throwable {
        this.driver.findElement(By.xpath(".//*[@id=\'salesforceLogo\']"));
    }

    @When("^I Click on \"([^\"]*)\"$")
    public void i_Click_on(String Link) throws Throwable {
        this.driver.findElement(By.id(Link)).click();
    }

    @Then("^I am on \"([^\"]*)\"$")
    public void i_am_on(String Tab) throws Throwable {
        this.driver.findElement(By.id(Tab));
    }

    @Then("^application should be closed$")
    public void application_should_be_closed() throws Throwable {
        this.driver.quit();
    }
}

编码一个 Map,其中键作为 link 的名称,值作为 link 的 xpath。使用功能文件中的密钥并访问步骤定义中的 xpath。

    private static Map<String, String> links = new HashMap<>();
    links.put("Home","//*[@id='oneHeader']/div[3]/div/div[2]/nav/ul/li[1]/a/span");
    ....
    ....

如果您想使用其他混合定位器策略,例如 id、名称、css 等,请考虑创建一个 class 并在地图中使用它而不是值。

public class LocatorObject {

     private By by;
     private String locator;

     public LocatorObject(By by, String loc) { // usual stuff }
}

links.put("Home", new LocatorObject (By.xpath, "//*[@id='oneHeader']/div[3]/div/div[2]/nav/ul/li[1]/a/span"));

查看 ScenarioOutline 而不是编写多个类似的场景。

Scenario Outline:
    Given I am on Homepage
    When I Click on <Link>
    Then I am on <Validate>
    And application should be closed

    Examples:
    | Link | Validate |
    | Home | Home |
    | Calender | Calender |
    | Leads | Leads |
    etc etc

您在这里所做的只是测试主页的导航。这是一种价值很低的测试,它需要大量的工作,而且你不会从中得到太多。也就是说,有时我会写这些场景。这是我的做法。

Feature: Navigation from home page
  As a user 
  I want to be able to get to important places from the home page
  So I can use the home page as a starting point

Background: 
  Given I am on the homepage

Scenario: Navigate to contacts
  When I navigate to contacts
  Then I should see my contacts

我会为每条导航做一个场景,因为

  1. 清晰多了
  2. 减少步骤定义的数量相对不重要

我将按如下方式实现步骤定义(注意这是在 ruby 中,您必须转换为 java。

# features/step_definitions/homepage/navigation_steps.rb

When 'I navigate to contacts' do
  click_link '.nav-to-contacts'
end

同样,我将为每件事定义一个步骤,因为它简单得多。 请注意我是如何使用 css 进行点击的,这意味着如果有人出现并更改文本,比如 'My Contacts',我们不必更新我们的场景。

最后我会为“thens”做同样的事情

Then 'I should see my contacts' do
  expect(page).to have_css('.contacts')
end

几点:

  • 您不需要在每种情况下都关闭应用程序。它既乏味又不必要。 Cucumber 为您做这件事。
  • 在场景中使用正则表达式通常会适得其反。一个场景简单明了比简洁重要得多。这尤其适用于场景失败的情况。
  • 包括'How'事情都在场景中完成是个坏主意。它使事情变得更加昂贵。当某些设计人员出现并将您的选项卡更改为其他 UI 控件时,此测试仍会完成其工作。您的场景中的内容包括:url 和点击
  • 你有一个完整的文件系统来存储你的功能和场景。明智地使用它来组织功能和步骤定义。 scenario/step_defintion 的位置应该告诉你很多关于它的功能。

最后,场景大纲是 Cucumber 中最被高估的部分之一,我强烈建议避免使用它们。它们减少了最不重要的代码,增加了复杂性并且更难实现。当一个失败时,诊断问题需要更长的时间。有 5 个简单的场景确实比一个复杂的场景要好得多。