1

セレンWebドライバーをセットアップする共通クラスを作成する必要があります。私のセットアップ基本クラス:Setupbase.java

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

しかし、このコードの実行中にエラーが発生します。誰でもこれに関して私を助けることができます。

4

2 に答える 2

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

これがあなたのために働くことを願っています。

于 2012-10-04T12:47:17.043 に答える
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();}

}
于 2012-10-04T06:16:58.157 に答える