Selenium,Arquillian,Eclipse:尝试让测试等待,直到页面完全加载

时间:2013-11-19 09:22:09

标签: java selenium junit jboss jboss-arquillian

这是我第一次在这里发帖。自从我开始使用Selenium和Arquillian来测试我们的网站仅仅几天......所以请耐心等待,因为我仍然缺乏很多知识......

我正在尝试通过键入四种不同的用户名和密码组合进行登录测试。其中三个不正确,当提交这些用户名和密码时,网站应显示错误消息。测试应检查是否确实显示了错误消息。请注意,错误消息只是同一网站中的文本框(不是弹出窗口或类似的东西......)

在输入下一个用户名/密码组合之前,我想首先刷新网站。否则,测试将始终检测前一个条目的错误消息。问题是在提交第一个用户名/密码并刷新页面后,测试才会停止。在JUnit中,我收到了以下错误消息:“org.openqa.selenium.StaleElementReferenceException:”在缓存中找不到元素 - 可能自查找以来页面已更改“。

我猜测刷新测试后应等待几秒钟直到页面完全加载..?但是,如果我使用“fluentWait”和“findElement(By.id(”user_name“))”。测试将继续运行(它不会等待),因为两个会话都是相同的网站(刷新前和刷新后),id“user_name”当然总是存在...

我真的很感激任何帮助:)

package org.xxyy.uitest;

import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;

//import org.openqa.selenium.firefox.FirefoxDriver;

@RunWith(Arquillian.class)
public class LoginTest {

    static String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Drone
    WebDriver driver;

    @BeforeClass
    public static void setUp() throws Exception {
        baseUrl = "http://team-xxyy.intern/";
    }

    @Before
    public void beforeTest() throws Exception {
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get(baseUrl + "xxyy/login.html");
    }

    @Test
    public void fehlLogin1() throws Exception {


        WebElement username = driver.findElement(By.id("user_name"));
        WebElement password = driver.findElement(By.id("user_password"));

        username.clear();
        username.sendKeys("blume");
        password.clear();
        password.sendKeys("homersimpson");
        driver.findElement(By.id("login_submit")).click();
        Thread.sleep(2000);
        verifyLoginInfo("blume", "homersimpson");

    }

    @Test
    public void fehlLogin2() throws Exception {


        WebElement username = driver.findElement(By.id("user_name"));
        WebElement password = driver.findElement(By.id("user_password"));

        username.clear();
        username.sendKeys("blume");
        password.clear();
        password.sendKeys("jill");
        driver.findElement(By.id("login_submit")).click();
        Thread.sleep(2000);
        verifyLoginInfo("blume", "jill");

    }

    @Test
    public void fehlLogin3() throws Exception {

        WebElement username = driver.findElement(By.id("user_name"));
        WebElement password = driver.findElement(By.id("user_password"));

        username.clear();
        username.sendKeys("jack");
        password.clear();
        password.sendKeys("homersimpson");
        driver.findElement(By.id("login_submit")).click();
        Thread.sleep(2000);
        verifyLoginInfo("jack", "homersimpson");

    }

    @Test
    public void fehlLogin4() throws Exception {


        WebElement username = driver.findElement(By.id("user_name"));
        WebElement password = driver.findElement(By.id("user_password"));

        username.clear();
        username.sendKeys("jack");
        password.clear();
        password.sendKeys("jill");
        driver.findElement(By.id("login_submit")).click();
        Thread.sleep(2000);
        verifyLoginInfo("jack", "jill");

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private void verifyLoginInfo(String user_name, String user_password) {

        if (user_name.equals("jack")
                && user_password.equals("jill")
                && driver.getCurrentUrl().startsWith(
                        "https://global.something.com/login/123456789/")) {
            System.out
                    .println("XXYY bitte manuell einloggen. Test wird abgebrochen");
            return;

        }

        if (user_name.equals("jack") && user_password.equals("jill")) {

            if (isForwardSuccessful()) {
                System.out.println("Eingabe: Username = " + user_name
                        + ", Passwort = " + user_password);
                System.out.println("Korrekt-Login: Erfolgreich!");
                return;
            } else {
                System.out.println("Eingabe: Username = " + user_name
                        + ", Passwort = " + user_password);
                System.out
                        .println("Korrekt-Login: Trotz korrekter Benutzername und Passwort ist Login fehlgeschlagen.");
                return;
            }
        }

        if (isAlertPresent()) {
            System.out.println("Eingabe: Username = " + user_name
                    + ", Passwort = " + user_password);
            System.out.println("Fehl-Login: Erfolgreich!");
            return;

        } else {
            System.out.println("Eingabe: Username = " + user_name
                    + ", Passwort = " + user_password);
            System.out
                    .println("Fehl-Login: Fehlermeldung wird nicht angezeigt.");
            return;

        }

    }

    private boolean isAlertPresent() {
        try {
            assertEquals(
                    "Der Benutzername oder das Passwort sind nicht korrekt. Bitte versuchen Sie es erneut.",
                    driver.findElement(
                            By.cssSelector("#password-message > span.message"))
                            .getText());
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    private boolean isForwardSuccessful() {
        try {
            assertEquals(
                    "http://team-xxyy.intern/xxyy/index.html",
                    driver.getCurrentUrl());
            return true;
        } catch (Error e) {
            verificationErrors.append(e.toString());
            return false;
        }

    }

}

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.xxyy.uitest</groupId>
    <artifactId>xxyy-ui-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>xxyy-ui-test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 20131112 die beiden Zeilen unten wurden neu hinzugefügt von "https://docs.jboss.org/author/display/ARQ/Drone" -->
        <version.org.jboss.arquillian>1.1.1.Final</version.org.jboss.arquillian>
        <version.org.jboss.arquillian.drone>1.2.0.CR1</version.org.jboss.arquillian.drone>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>${version.org.jboss.arquillian}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian.extension</groupId>
                <artifactId>arquillian-drone-bom</artifactId>
                <version>${version.org.jboss.arquillian.drone}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- 20131113 Version hab ich erstmal rauskommentiert -->
                <!--  version2.3.2version-->
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-drone-impl</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 20131112 ab hier neu hinzugefügt von "https://docs.jboss.org/author/display/ARQ/Drone" -->
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-drone-webdriver-depchain</artifactId>
            <version>${version.org.jboss.arquillian.drone}</version>
            <type>pom</type>
            <scope>test</scope>
        </dependency>
        <!-- bis hier -->
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-drone-selenium</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-drone-selenium-server</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>servlet-api-2.5</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <profiles>
        <profile>
            <id>arquillian-weld-ee-embedded</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.jboss.spec</groupId>
                    <artifactId>jboss-javaee-6.0</artifactId>
                    <version>1.0.0.Final</version>
                    <type>pom</type>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>org.jboss.arquillian.container</groupId>
                    <artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
                    <version>1.0.0.CR3</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.jboss.weld</groupId>
                    <artifactId>weld-core</artifactId>
                    <version>1.1.5.Final</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                    <version>1.6.4</version>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>arquillian-glassfish-embedded</id>
            <dependencies>
                <dependency>
                    <groupId>org.jboss.arquillian.container</groupId>
                    <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
                    <version>1.0.0.CR3</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.glassfish.main.extras</groupId>
                    <artifactId>glassfish-embedded-all</artifactId>
                    <version>3.1.2</version>
                    <scope>provided</scope>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>arquillian-jbossas-managed</id>
            <dependencies>
                <dependency>
                    <groupId>org.jboss.spec</groupId>
                    <artifactId>jboss-javaee-6.0</artifactId>
                    <version>1.0.0.Final</version>
                    <type>pom</type>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>org.jboss.as</groupId>
                    <artifactId>jboss-as-arquillian-container-managed</artifactId>
                    <version>7.1.1.Final</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>org.jboss.arquillian.protocol</groupId>
                    <artifactId>arquillian-protocol-servlet</artifactId>
                    <scope>test</scope>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
</project>

arquillian.xml

<arquillian xmlns="http://jboss.com/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <extension qualifier="webdriver">
        <property name="browser">firefox</property>
    </extension>

</arquillian>

1 个答案:

答案 0 :(得分:0)

好的,对您的代码的评论很少

  • driver = new FirefoxDriver(); - 您不应该自己实例化驱动程序,@Drone会为您执行此操作
  • 你应该将你的测试分成4个方法(你正在测试4个测试用例,对吗?)并引入用@Before注释的方法,它可以在测试之间刷新页面(或者调用driver.get("baseUrl + "xxyy/login.html") - 这将使你的代码更简洁和可读
  • 你也可以提取测试方法的内容并将它放在一些util方法中,大多数代码都在每个测试中重复,所以这里的一些重构会很好:)

尝试这样,看看会发生什么:)