2012-10-04 77 views
1

我必須創建一個通用類,它設置硒webdriver。我設置的基類:Setupbase.javaselenium網絡驅動程序:如何從硒調用一個類Junit程序

public void setUp() throws Exception { 
driver = new FirefoxDriver(); 
baseUrl = "http://example.com/"; 
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);} 

此設置類是常見的。每當我寫一個新程序時,我都需要調用這個類。 這是我的登錄程序: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();} 

但同時執行此代碼我收到提示。誰能幫我解決這個問題。

回答

1

這將是你設置類:

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);} 

} 

使用該設置類的類:

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();} 

} 
+0

driver.get(baseUrl +「/」);從這行不工作..空webdriver窗口已打開..但baseurl沒有加載.. – bumblebee87

+0

要麼不添加'/'在您的基地網址字符串或刪除'/'你的baseurl字符串在setupbase class.See編輯的答案。 –

1

我謹代表我的結構我在項目中使用。 看來你忘了@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)); 

    } 
.... 
} 

然後我致以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")); 

    } 
..... 
} 

所以從你的代碼點它是這樣的:

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(); 
    } 
} 

希望這對你的作品。