【发布时间】:2017-11-27 18:33:43
【问题描述】:
1) 运行 TestRunner 执行测试时,功能文件未连接步骤定义代码并给出以下消息:
Feature: Post text Hello Sakshi on Sakshi facebook account
Scenario: Login sucessfully on Facebook application [90m# MyApplication.feature:2[0m
[33mGiven [0m[33mOpen Facebook application[0m
[33mWhen [0m[33mEnter valid id and password[0m
[33mAnd [0m[33mClick on Login button[0m
[33mThen [0m[33mFacebook home page should open[0m
Scenario: Type text Sakshi Tyagi in search box [90m# MyApplication.feature:8[0m
[33mGiven [0m[33mUser on Facebook home page[0m
[33mWhen [0m[33mEnter text Sakshi Tyagi in search box[0m
[33mAnd [0m[33mClick search button[0m
[33mThen [0m[33mProfile of Sakshi should open[0m
Scenario: Type text Hello Sakshi in post box [90m# MyApplication.feature:14[0m
[33mGiven [0m[33mUser in Sakshi s facebook page[0m
[33mWhen [0m[33mEnter text Hello Sakshi in search box[0m
[33mAnd [0m[33mClick post button[0m
[33mThen [0m[33mMessage should post to Sakshi[0m
3 Scenarios ([33m3 undefined[0m)
12 Steps ([33m12 undefined[0m)
0m0.000s
You can implement missing steps with the snippets below:
@Given("^Open Facebook application$")
public void open_Facebook_application() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Enter valid id and password$")
public void enter_valid_id_and_password() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Click on Login button$")
public void click_on_Login_button() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^Facebook home page should open$")
public void facebook_home_page_should_open() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Given("^User on Facebook home page$")
public void user_on_Facebook_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Enter text Sakshi Tyagi in search box$")
public void enter_text_Sakshi_Tyagi_in_search_box() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Click search button$")
public void click_search_button() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^Profile of Sakshi should open$")
public void profile_of_Sakshi_should_open() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Given("^User in Sakshi s facebook page$")
public void user_in_Sakshi_s_facebook_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Enter text Hello Sakshi in search box$")
public void enter_text_Hello_Sakshi_in_search_box() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("^Click post button$")
public void click_post_button() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@Then("^Message should post to Sakshi$")
public void message_should_post_to_Sakshi() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
2) 包含场景的特征文件
Feature: Post text Hello Sakshi on Sakshi facebook account
Scenario: Login sucessfully on Facebook application
Given Open Facebook application
When Enter valid id and password
And Click on Login button
Then Facebook home page should open
Scenario: Type text Sakshi Tyagi in search box
Given User on Facebook home page
When Enter text Sakshi Tyagi in search box
And Click search button
Then Profile of Sakshi should open
Scenario: Type text Hello Sakshi in post box
Given User in Sakshi s facebook page
When Enter text Hello Sakshi in search box
And Click post button
Then Message should post to Sakshi
3) TestRunner:这是测试的主要主运行器
package testRunner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty","html:target/cucumber-html-report"},
features = {"features"},
glue = {"classpath:src/stepDefiniction"},
monochrome = false,
strict = false,
dryRun = false)
public class TestRunnner {
}
4) 步骤定义:这些是浏览器自动化的主要步骤
package stepDefinition;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
//import stepDefiniction.EncodingAndDecodingClass;
public class StepDefinition {
private static WebDriver driver = null ;
//private static String decPassword = "SG90Zml4QDIyMg==";
//String strPassword = EncodingAndDecodingClass.decodingMethod (decPassword);
private static String password = "MindLift@xxx";
WebDriverWait wait=new WebDriverWait(driver, 30);
WebElement waitElement;
// First scenario
@Given("^Open Facebook application$")
public void open_Facebook_application() throws Throwable {
driver = new FirefoxDriver ();
driver.navigate().to("https://www.facebook.com/");
driver.manage().window().maximize();
}
@When("^Enter valid id and password$")
public void enter_valid_id_and_password() throws Throwable {
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys("rafiras16@gmail.com");
driver.findElement(By.name("pass")).clear();
driver.findElement(By.name("pass")).sendKeys(password);
}
@When("^Click on Login button$")
public void click_on_Login_button() throws Throwable {
driver.findElement(By.xpath("//input[starts-with(@id,'u_0')]")).click();
}
@Then("^Facebook home page should open$")
public void facebook_home_page_should_open() throws Throwable {
String strTitle = driver.getTitle();
System.out.print(strTitle);
}
// Second scenario
@Given("^User on Facebook home page$")
public void user_on_Facebook_home_page() throws Throwable {
waitElement= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[(@class='_1frb') and (@name='q')]")) );
}
@When("^Enter text Sakshi Tyagi in search box$")
public void enter_text_Sakshi_Tyagi_in_search_box() throws Throwable {
waitElement.clear();
waitElement.sendKeys("Sakshi Tyagi");
}
@When("^Click search button$")
public void click_search_button() throws Throwable {
waitElement.sendKeys(Keys.RETURN);
}
@Then("^Profile of Sakshi should open$")
public void profile_of_Sakshi_should_open() throws Throwable {
}
// Third scenario
@Given("^User in Sakshi s facebook page$")
public void user_in_Sakshi_s_facebook_page() throws Throwable {
waitElement= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[(@class='_52eh _5bcu') and (text()='Sakshi Tyagi')]")) );
waitElement.click();
}
@When("^Enter text Hello Sakshi in search box$")
public void enter_text_Hello_Sakshi_in_search_box() throws Throwable {
waitElement= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@data-testid='status-attachment-mentions-input']")) );
waitElement.sendKeys("Hello ma'am");
}
@When("^Click post button$")
public void click_post_button() throws Throwable {
waitElement= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[(@type='submit') and (@data-testid='react-composer-post-button')]")));
waitElement.click();
}
@Then("^Message should post to Sakshi$")
public void message_should_post_to_Sakshi() throws Throwable {}
} // end of definition steps
5) 文件夹结构图片:
【问题讨论】:
-
尝试使用胶水=“stepDefinition”。目前你有一个额外的“c”。
-
我已经删除了那个'c',但问题还是一样。仅限“CucumberProject”项目文件夹中的“features/MyApplication.feature”。无法找出确切的原因并重建了两次,但它不起作用。任何帮助将不胜感激,谢谢。
-
你试过我建议的胶水选项了吗?胶水=“ste....”
-
是的,我尝试了相同的胶水 = "stepDefinition" 但问题仍然相同,请协助。
-
尝试将功能文件移动到 src 文件夹中。