【问题标题】:Selenium, Arquillian, Eclipse: trying to make test wait until the page is fully loadedSelenium,Arquillian,Eclipse:试图让测试等到页面完全加载
【发布时间】:2013-11-19 09:22:08
【问题描述】:

这是我第一次在这里发帖。距离我开始使用 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>

【问题讨论】:

    标签: java selenium junit jboss jboss-arquillian


    【解决方案1】:

    好的,你的代码有几个 cmets

    • driver = new FirefoxDriver(); - 你永远不应该自己实例化驱动程序,@Drone 会为你做到这一点
    • 您应该将您的测试分成 4 个方法(您正在测试 4 个测试用例,对吗?)并引入带有 @Before 注释的方法,它可以在测试之间刷新页面(或调用 driver.get("baseUrl + "xxyy/login.html") - 这将使您的代码更加简洁易读
    • 你也可以把测试方法的内容拉出来放到某个 util 方法中,大部分代码在每个测试中都在重复,所以在这里进行一些重构会很好:)

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

    【讨论】:

    • 您好,感谢您这么快回复!我尝试一个接一个地实施更改。首先,我刚刚摆脱了“driver = new FirefoxDriver();”我注意到即使在提交了正确的用户名/密码后测试仍在继续。当我显式实例化驱动程序(firefox)时,测试实际上在正确的用户名/密码后停止并且浏览器已转发到主页(索引)
    • ..我似乎无法使用@BeforeMethod。我在 Eclipse 中得到“BeforeMethod”无法解析为类型”
    • 就像我说的,在将代码拆分为多个测试之前,很难分辨出哪里出了问题。但是自己实例化驱动肯定是不对的
    • 我已将我的代码分成 4 个测试并再次运行测试类。不知何故,只有第一个测试成功执行......之后测试停止,我在 JUnit 中收到此错误消息:“org.openqa.selenium.WebDriverException:org.openqa.selenium.remote.SessionNotFoundException:会话已关闭”。我的测试代码现在看起来像这样......
    • arquillian.xml 中有浏览器属性吗? docs.jboss.org/author/display/ARQGRA2/Using+Drone
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2016-08-04
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多