JUnit runner class 无法执行@before
JUnit runner class unable to execute @before
下面是项目结构:
project structure
Before After class
Test Runner class:
package runnerPackage;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions( plugin = {"html:target/cucumber-html-report",
"json:target/cucumber.json",
"pretty:target/cucumber-pretty.txt",
"usage:target/cucumber-usage.json"
},
features="classpath:MyFirstApp.feature",glue={"stepDefinitions"},tags=
{"@sceneOne1"})
public class TestRunner {
}
Feature file :
@tag
Feature: Test HDFC
@sceneOne1
Scenario: Test HDFC
Given I open HDFC Bank home page and click on 'Login' button
***********************************************
Step Definition :
package stepDefinitions;
import pages.MyFirstMethod;
import cucumber.api.java.en.Given;
public class MyFirstStepDefinition {
MyFirstMethod m=new MyFirstMethod();
@Given("^I open HDFC Bank home page and click on 'Login' button$")
public void iOpenHDFCBankHomePageAndClickOnLoginButton() throws
Throwable {
m.clickOnElement();
}
}
**********************************************
Before After class:
package runnerPackage;
import org.openqa.selenium.chrome.ChromeDriver;
import pages.ConfigReadFile;
import pages.PageInstances;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class BeforeAfter 扩展了 PageInstances{
@Before
public static void runBefore(Scenario scenario)
{
System.setProperty("webdriver.chrome.driver", "D:\chromedriver_win32_2.26\chromedriver.exe");
System.out.println("reached here");
driver=new ChromeDriver();
System.out.println(ConfigReadFile.URL);
driver.get(ConfigReadFile.URL);
}
@After
public static void runAfter()
{
System.out.println("do noting");
}
}
**********************************************************
Page Instances:
package pages;
import org.openqa.selenium.WebDriver;
public class PageInstances {
protected static WebDriver driver;
}
方法:
包裹页面;
进口java.util.concurrent.TimeUnit;
进口org.openqa.selenium.By;
导入 org.openqa.selenium.interactions.Actions;
public class MyFirstMethod 扩展了 PageInstances{
public void clickOnElement()
{
Actions action=new Actions(driver);
action.moveToElement(driver.findElement(By.xpath(".//*[@id='cee_closeBtn']/img[@alt='Close']"))).click();
action.perform();
//driver.findElement(By.xpath(".//*[@id='cee_closeBtn']/img[@alt='Close']")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.findElement(By.id("loginsubmit")).click();
System.out.println(driver.getCurrentUrl());
//loginButton.click();
}
}
Configuration xml:
<ConfigurationFile.xml>
<url>some random url</url>
</ConfigurationFile.xml>
配置读取文件:
package pages;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
public class ConfigReadFile {
public static final String URL;
static {
File f=new File("file path");
DocumentBuilderFactory
docbuildFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = docbuildFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(f);
} catch(Exception es)
{
}
URL=doc.getElementsByTagName("url").item(0).getTextContent();
System.out.println(URL);
}
}
我想为每个场景打开 chrome 浏览器和 hdfc link 所以我用 @before
注释了它
应该提到 glue={"stepDefinitions","runnerPackage"} 以便加载挂钩 class
下面是项目结构:
project structure
Before After class
Test Runner class:
package runnerPackage;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions( plugin = {"html:target/cucumber-html-report",
"json:target/cucumber.json",
"pretty:target/cucumber-pretty.txt",
"usage:target/cucumber-usage.json"
},
features="classpath:MyFirstApp.feature",glue={"stepDefinitions"},tags=
{"@sceneOne1"})
public class TestRunner {
}
Feature file :
@tag
Feature: Test HDFC
@sceneOne1
Scenario: Test HDFC
Given I open HDFC Bank home page and click on 'Login' button
***********************************************
Step Definition :
package stepDefinitions;
import pages.MyFirstMethod;
import cucumber.api.java.en.Given;
public class MyFirstStepDefinition {
MyFirstMethod m=new MyFirstMethod();
@Given("^I open HDFC Bank home page and click on 'Login' button$")
public void iOpenHDFCBankHomePageAndClickOnLoginButton() throws
Throwable {
m.clickOnElement();
}
}
**********************************************
Before After class:
package runnerPackage;
import org.openqa.selenium.chrome.ChromeDriver;
import pages.ConfigReadFile;
import pages.PageInstances;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class BeforeAfter 扩展了 PageInstances{
@Before
public static void runBefore(Scenario scenario)
{
System.setProperty("webdriver.chrome.driver", "D:\chromedriver_win32_2.26\chromedriver.exe");
System.out.println("reached here");
driver=new ChromeDriver();
System.out.println(ConfigReadFile.URL);
driver.get(ConfigReadFile.URL);
}
@After
public static void runAfter()
{
System.out.println("do noting");
}
}
**********************************************************
Page Instances:
package pages;
import org.openqa.selenium.WebDriver;
public class PageInstances {
protected static WebDriver driver;
}
方法: 包裹页面;
进口java.util.concurrent.TimeUnit;
进口org.openqa.selenium.By; 导入 org.openqa.selenium.interactions.Actions;
public class MyFirstMethod 扩展了 PageInstances{
public void clickOnElement()
{
Actions action=new Actions(driver);
action.moveToElement(driver.findElement(By.xpath(".//*[@id='cee_closeBtn']/img[@alt='Close']"))).click();
action.perform();
//driver.findElement(By.xpath(".//*[@id='cee_closeBtn']/img[@alt='Close']")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.findElement(By.id("loginsubmit")).click();
System.out.println(driver.getCurrentUrl());
//loginButton.click();
} }
Configuration xml:
<ConfigurationFile.xml>
<url>some random url</url>
</ConfigurationFile.xml>
配置读取文件:
package pages;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
public class ConfigReadFile {
public static final String URL;
static {
File f=new File("file path");
DocumentBuilderFactory
docbuildFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = docbuildFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(f);
} catch(Exception es)
{
}
URL=doc.getElementsByTagName("url").item(0).getTextContent();
System.out.println(URL);
}
}
我想为每个场景打开 chrome 浏览器和 hdfc link 所以我用 @before
注释了它应该提到 glue={"stepDefinitions","runnerPackage"} 以便加载挂钩 class