需要传递多个链接参数才能在黄瓜中测试(Need to pass parameter for multiple links to test in cucumber)

编程入门 行业动态 更新时间:2024-10-26 06:34:11
需要传递多个链接参数才能在黄瓜中测试(Need to pass parameter for multiple links to test in cucumber)

我已经编写了这个功能文件来测试主页中的多个链接。我试图通过功能文件传递参数来减少步骤定义的数量。 我面临的问题,在功能文件中可以获取每个标签的元素名称。没有提到的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(); } }

I have written this feature file for testing multiple links in home page.I was trying to reduce number of step definitions by trying to pass parameter through feature file. I am facing problem in writing element name in feature file which can fetch every tab.There is no id mentioned so I had to take xpath(which I know is not ideal to mention in feature file" .Can u suggest any alternate way to it?

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

Step Definition look like this

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(); } }

最满意答案

你在这里做的只是测试主页上的导航。 这是一个非常低价值的测试,需要很多工作,而且你没有太多的工作。 这就是说我有时会写这些场景。 我就是这样做的。

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

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

它更清晰 减少步骤定义的数量相对不重要

我将如下实现步骤定义(注意这是ruby,你必须转换为java。

# features/step_definitions/homepage/navigation_steps.rb When 'I navigate to contacts' do click_link '.nav-to-contacts' end

再次,我会为每件事物做一个定义,因为它简单得多。 请注意我是如何使用css进行点击的,这意味着如果有人出现并更改文本,请说“我的联系人”,我们不必更新我们的方案。

最后我会为`thens'做同样的事情

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

一些观点:

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

最后,场景大纲是最受高估的黄瓜片之一,我强烈建议避免它们。 它们减少了代码,在其中最不重要的地方,增加了复杂性,并且实施起来更加困难。 当发生故障时,诊断问题需要更长的时间。 拥有5个简单场景而不是一个复杂场景确实要好得多。

What you are doing here is just testing navigation from the homepage. This is a pretty low value sort of test, it takes quite a bit of work and you don't get that much out of it. That said sometimes I write these sorts of scenarios. Here is how I'd do it.

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

I would do a scenario for each piece of navigation becuase

Its much clearer Reducing the number of step definitions is relatively unimportant

I'd implement the step definitions as follows (note this is in ruby, you'll have to translate to java.

# features/step_definitions/homepage/navigation_steps.rb When 'I navigate to contacts' do click_link '.nav-to-contacts' end

Again I'll do a step definition for each thing because it is just so much simpler. Notice how i'm using css for the clicking, these means that if someone comes along and changes the text, say to 'My Contacts', we don't have to update our scenario.

Finally I'll do the same for the `thens'

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

Some points:

you don't need to close the application in every scenario. Its tedious and unnecessary. Cucumber does this for you. using regex's in scenarios is often counter-productive. Its much more important for a scenario to be simple and clear than it is for it to be concise. This particularly applies when the scenario fails. including 'How' things are done in scenarios is a bad idea. It makes things much more expensive to change. This test will still do its jobs when some designer comes along and changes your tabs to some other UI control. How things in your scenario include: the url's and clicking you have a whole file system to store your features and scenarios. Use it wisely to organise both features and step definitions. The location of a scenario/step_defintion should tell you alot about its function.

Finally scenario outlines are one of the most overrated pieces of Cucumber, I'd strongly recommend avoiding them. They reduce code, where its least important, add complexity and are much harder to implement. When one fails it takes much longer to diagnose the problem. It really is much better to have 5 simple scenarios than one complex one.

更多推荐

本文发布于:2023-08-05 04:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   能在   黄瓜   参数   链接

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!