java selenium web 驱动程序:如何从 selenium Junit 程序调用一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12721192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
selenium web driver: How to call a class from selenium Junit program
提问by bumblebee87
I have to create a common class which setup the selenium webdriver. My setup base class :Setupbase.java
我必须创建一个通用类来设置 selenium webdriver。我的设置基类:Setupbase.java
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://example.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}
This setup class is common. Whenever I write a new program I need to call this class. This is my login program: Login.java
这个安装类很常见。每当我编写一个新程序时,我都需要调用这个类。这是我的登录程序:Login.java
public class Login extends Setupbase{
super.setUp();
driver.get(baseUrl + "/");
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("username");
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();}
But i'm getting error while executing this code. can anyone help me regarding this.
但是在执行此代码时出现错误。任何人都可以帮我解决这个问题。
回答by Abhishek_Mishra
This will be your setup class :
这将是您的设置类:
public class Setupbase {
WebDriver driver;
String baseUrl;
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://example.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);}
}
The class that uses that setup class:
使用该设置类的类:
public class Login extends Setupbase
{
@Test
public void LoginTest() throws Exception{
super.setUp();
driver.get(baseUrl + "/");
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("username");
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();}
}
回答by eugene.polschikov
I would like to represent my structure I use in my project.
It seems you forgot @Before
, @After
and @Test
notation.
我想代表我在项目中使用的结构。看来你忘了@Before
,@After
和 @Test
符号。
public class BaseSeleniumTest extends SeleneseTestBase {
static WebDriver driver;
@Before
public void openFirefox() throws IOException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(propertyKeysLoader("login.base.url"));
doAdminLogin();
}
@After
public void closeFirefox(){
driver.quit();
}
public void doAdminLogin() throws IOException {
String curTitle=driver.getTitle();
locatorFindingHandling("login.logininput", "login.admin.login");
locatorFindingHandling("login.passinput", "login.admin.pass");
locatorFindingHandling("login.loginbutton");
String newTitle=driver.getTitle();
Assert.assertFalse(curTitle.equals(newTitle));
}
public void locatorFindingHandling(String key) throws IOException /*throws IOException*/ {
fluentWait(By.cssSelector(propertyKeysLoader(key))).click();
}
public void locatorFindingHandling(String key, String key1) throws IOException {
driver.findElement(By.xpath(propertyKeysLoader(key))).sendKeys(propertyKeysLoader(key1));
}
public void doLogout() throws InterruptedException, IOException {
String curTitle=driver.getTitle();
jsClick("rms.home.logout");
String newTitle=driver.getTitle();
Assert.assertFalse(curTitle.equals(newTitle));
}
....
}
And then I extend my BaseSeleniumTest.java
in the following way:
然后我BaseSeleniumTest.java
以以下方式扩展我的:
public class LoginPageTestSuite extends BaseSeleniumTest {
@Test
public void loginWithEmptyCredentials() throws IOException, InterruptedException {
doLogout();
fluentWait(By.cssSelector(propertyKeysLoader("login.loginbutton"))).click();
Assert.assertTrue(fluentWait(By.cssSelector(propertyKeysLoader("login.validator.invalidautentication"))).getText().trim().equals("Invalid authentication"));
}
@Test
public void logoutAdminLogin() throws IOException, InterruptedException {
doLogout();
doAdminLogin();
}
@Test
public void loginWithWrongPass() throws IOException, InterruptedException {
doLogout();
locatorFindingHandling("login.logininput", "login.admin.login");
locatorFindingHandling("login.passinput", "login.invalidPass");
locatorFindingHandling("login.loginbutton");
Assert.assertTrue(fluentWait(By.cssSelector(propertyKeysLoader("login.validator.invalidautentication"))).getText().trim().equals("Invalid authentication"));
}
.....
}
So from the point of your code it be something like:
所以从你的代码来看,它是这样的:
public class Setupbase extends SeleneseTestBase {
static WebDriver driver;
@Before
public void openFirefox() throws IOException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String baseUrl = "http://example.com";
driver.get(baseUrl);
}
@After
public void closeFirefox(){
driver.quit();
}
}
public class Login extends Setupbase{
@Test
public void loginTest() {
driver.findElement(By.id("Email")).clear();
driver.findElement(By.id("Email")).sendKeys("username");
driver.findElement(By.id("Passwd")).clear();
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("signIn")).click();
}
}
Hope this works for you.
希望这对你有用。