我有一个黄瓜特征文件,其中有一组场景大纲。对于场景大纲中的每个示例,都有相应的步骤定义。但问题是每个示例都有不同的xpath,需要迭代固定次数。对于需要执行固定次数的特定数量的步骤,我无法在Java步骤定义文件中放置一个循环。提前感谢您的帮助
我试图把循环放在步骤定义方法之前,这样我就可以调用循环中的方法,但总是得到意外的token
错误。以下是定义的场景步骤集:'
- 当我点击过滤器图标 然后输入
- 选择specific
- 再次输入 然后点击滤镜按钮
- 那么网格应该相应地进行过滤 然后点击清除滤镜按钮
- 然后我点击全部折叠按钮'
我的查询是哪个步骤的定义应该放置循环,因为这些是重复的步骤超过8次。
下面是步骤定义的代码片段:@When("I click on {} filter icon")
public void filterIcon(String columnHeader) {
// determine row
driver.findElement(By.xpath("//div[@id='grid']//table[@role='grid']/thead[@role='rowgroup']/tr[@role='row']/th[2]//span")).click();
}
@And("I enter the {}")
public void enterTextOne(String textOne) {
//some name one
Actions act1 = new Actions(driver);
WebElement val1 = driver.findElement(By.xpath("/html//form[@title='Show items with value that:']//input[@title='Value']"));
act1.moveToElement(val1).click(val1).sendKeys(textOne).perform();
}
@And("I select specific {}")
public void selectSpecificLogic(String logic) {
//and or or
WebDriverWait pause = new WebDriverWait(driver, Duration.ofSeconds(3000));
WebElement filterLogic = driver.findElement(By.xpath("/html//form[@title='Show items with value that:']/div[1]/span[2]//span[@class='k-input']"));
filterLogic.click();
WebElement logic1 = pause.until(
ExpectedConditions.elementToBeClickable(
driver.findElement(By.xpath(
"/html//form[@title='Show items with value that:']/div[1]/span[2]//span[@class='k-input']"))));
Actions act3 = new Actions(driver);
act3.moveToElement(logic1).click(logic1).perform();
}
@And("I enter again{}")
public void enterTextTwo(String textTwo) {
//enter the second param here
Actions act2 = new Actions(driver);
WebElement val2 = driver.findElement(By.xpath("/html/body/div[7]/form/div[1]/input[2]"));
act2.moveToElement(val2).click(val2).sendKeys(textTwo).perform();
}
@And("I click on Filter button")
public void filterButton() {
driver.findElement(By.xpath("/html/body/div[@class='k-animation-container']/form[@title='Show items with value that:']//button[@type='submit']")).click();
}
@Then("Grid should be filtered accordingly")
public void verifyRow() throws InterruptedException {
//verifying row with data
//and this
WebDriverWait find = new WebDriverWait(driver, Duration.ofSeconds(10000));
WebElement gridData = driver.findElement(By.xpath("//*[@id='grid']/div/div[3]/table/tbody/tr[1]/td[1]/a"));
Thread.sleep(10000);
WebElement gridData1 = driver.findElement(By.xpath("//*[@id='grid']/div/div[3]/table/tbody/tr[1]/td[1]/a"));
WebElement grid = find.until(ExpectedConditions.elementToBeClickable(gridData1));
grid.click();
Thread.sleep(5000);
String cellData = driver.findElement(By.xpath("//tbody/tr[1]/td[1]/span[1]")).getText();
if (cellData != null) {
System.out.println("grid is filtered");
} else {
System.out.println("Grid not filtered");
}
}
@And("I click on Clear Filters button")
public void clearFiltersGrid() throws InterruptedException {
driver.findElement(By.xpath("//div[@id='grid']//ent-grid-buttons[@class='ng-isolate-scope']/ul[@class='ent-buttons-list']//a[.='Clear filters']")).click();
Thread.sleep(7000);
}
@And("I click on Collapse All button")
public void collapseGridRow() throws InterruptedException {
//collapsing the rows expanded in earlier grid filter
driver.findElement(By.xpath("//div[@id='grid']//ent-grid-buttons[@class='ng-isolate-scope']/ul[@class='ent-buttons-list']//a[@class='collapse-all-btn']")).click();
Thread.sleep(5000);
}
如果您在更高的抽象级别编写您的场景,您可以避免您面临的一些问题。
一般来说,在过滤时,你希望有些东西应该显示,有些东西不应该显示。你可以把你的过滤测试写成像
这样的东西Background:
Given I am logged in as an admin
Given there are some thing with foo enabled and foo disabled
And I am viewing all the things
Scenario: View all
Then I should see all the foo enabled and foo disabled things
Scenario: Filter by Foo
When I filter things to show foo enabled
Then I should see only the foo enabled things
And I should not see the foo disabled things
Scenario: Filter by No Foo
When I filter things to show foo disabled
Then I should see only the foo disabled things
And I should not see the foo enabled things
现在你的步骤在更大的块中工作,并且描述你正在做什么而不是你是如何做的。
通过使步骤定义调用helper方法而不是让它们包含所有代码,可以使步骤实现更简单。下面的示例是用Ruby编写的,但是您应该能够翻译
Given "there are some thing with foo enabled and foo disabled" do
@foo_enabled_things, @foo_disabled_things =
create_foo_enabled_and_disabled_things
end
这里我们将创建事物的工作委托给一个助手方法create_foo_enabled_and_disabled_things它返回两个数组我们将它们赋值给全局变量
现在唯一的困难是检查过滤器是否有效。在这里,您将循环存储在全局变量中的东西,以检查它们是否出现在视图中。那么你就会有
@foo.enabled_things.each do
...
end
...
我再次建议使用辅助方法,以便如何检查某事的详细信息
- 降低代码栈
- 在一个可以重复使用的地方
希望上面的内容能给你一些整理代码的想法,让事情变得更简单。好运。