[英]Design dependencies in JUnit Test Class
I am new to Junit and testing frameworks in general, here is my question. 我是Junit的新手并且测试框架一般,这是我的问题。
I have the following code: 我有以下代码:
public class Login extends WebsiteTestCase
{
@Test
public void testChromeLogin()
{
setDriver( getChromeDriver() );
login( getDriver() );
}
public void login( WebDriver driver )
{
driver.get( getBaseURL() );
WebElement username = driver.findElement( By.name("login_username") );
WebElement password = driver.findElement( By.name("login_password") );
WebElement submit = driver.findElement( By.cssSelector( "form#login input" ) );
username.sendKeys( getUsername() );
password.sendKeys( getPassword() );
submit.submit();
assertEquals( "Website HomePage", driver.getTitle() );
}
@After
public void tearDown() throws Exception
{
getDriver().quit();
}
}
and 和
public class WebsiteTestCase extends StockholmTestCase
{
public String username = "login", password_login = "password";
private String baseURL = "http://website.com";
public String getUsername()
{
return this.username;
}
public String getPassword()
{
return this.password_login;
}
public String getBaseURL()
{
return this.baseURL;
}
}
and 和
public class StockholmTestCase extends TestCase
{
private WebDriver driver;
public WebDriver getChromeDriver()
{
return new RemoteWebDriver( DesiredCapabilities.chrome() );
}
public WebDriver getFireFoxDriver()
{
return new RemoteWebDriver( DesiredCapabilities.firefox() );
}
public WebDriver getDriver()
{
return this.driver;
}
public void setDriver( WebDriver driver )
{
this.driver = driver;
}
}
As you can see in the WebsiteTestClass, I am hard-coding the current login username and password, and baseURL which works perfectly fine for my tests. 正如您在WebsiteTestClass中看到的,我正在对当前的登录用户名和密码进行硬编码,而baseURL对于我的测试来说非常合适。 But if I want to "programatically" set an environment for testing (perhaps for different programmers different login, password, different baseurl, ex: " http://Bob.dev.website.com "), then instead, I would like to be able to create an instance of WebsiteTestCase with different properties for login credentials, with a constructor.
但是如果我想“以编程方式”设置一个测试环境(可能是针对不同的程序员不同的登录,密码,不同的baseurl,例如:“ http://Bob.dev.website.com ”),那么相反,我想能够使用构造函数创建具有不同登录凭据属性的WebsiteTestCase实例。
like 喜欢
public class Stockholm
{
public static void main( String args[] )
{
JUnitCore junit = new JUnitCore();
Result result = junit.run( new Login( /* credentials here, settings */ ) );
}
}
as opposed to 而不是
public class Stockholm
{
public static void main( String args[] )
{
JUnitCore junit = new JUnitCore();
Result result = junit.run( Login.class );
}
}
I understand in the above code that I am not passing an instance of Login to junit.run, so how would I design code to accomplish those things in this context? 我在上面的代码中理解我没有将Login的实例传递给junit.run,那么我将如何在这种情况下设计代码来完成这些事情呢?
You can have property file in the project, like webdriver-test-dev.properties
and have the login credentials & URL defined in it. 您可以在项目中拥有属性文件,例如
webdriver-test-dev.properties
并在其中定义登录凭据和URL。 You can load that property file and inject properties during the test. 您可以在测试期间加载该属性文件并注入属性。
Environment can be passed as command line argument like -Denv=dev and using this value the right property file can be picked considering the file name if of a predefined format like webdriver-<env>.properties
. 环境可以像-Denv = dev一样作为命令行参数传递,并且使用此值可以考虑文件名来选择正确的属性文件,如果是
webdriver-<env>.properties
这样的预定义格式。
Also on a related not, for a test case it is better to use one common test login instead of having each programmer use his or her own login. 另外,对于测试用例,最好使用一个常见的测试登录,而不是让每个程序员使用他或她自己的登录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.