登录Selenium的麻烦

时间:2011-10-07 12:12:40

标签: java selenium webdriver

我正在尝试使用用户名“username”和密码“password”登录我们的生产网站,例如“http://site.com”。当我执行测试时,它会一直到站点打开的阶段,但是,它不会输入用户名和密码。代码如下:

package com.beginning;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.internal.runners.statements.Fail;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.Assert.*;
public class testcase {

    private WebDriver driver;
    private String baseUrl="";
    private StringBuffer verificationerrors=new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver=new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testJUnit0() throws Exception {
        driver.get("site.com");
        driver.findElement(By.name("j_username")).clear();
        //System.out.println("Element found");
        driver.findElement(By.name("j_username")).sendKeys("username");
        driver.findElement(By.name("j_password")).clear();
        driver.findElement(By.name("j_password")).sendKeys("password");
        driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationerrorString=verificationerrors.toString();

        }
    private boolean isElementPresent(By by)
    {
        try {
            driver.findElement(by);
            return true;
        }
        catch(NoSuchElementException e)
        {
            return false;
        }
    }
}

页面的相关HTML部分:

<form name="login" method="post" action="j_security_check">
                                        <table>
                                            <tbody>
                                                <tr>
                                                    <td>
                                                        <label style="color: gray; left: 0px; top: 48px;">
                                                            Username:
                                                        </label>
                                                    </td>
                                                    <td>
                                                        <input type="text" mandatory="true" value="" name="j_username" tabindex="1">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <label style="color: gray; left: 0px; top: 96px;">
                                                            Password:
                                                        </label>
                                                    </td>
                                                    <td>
                                                        <input type="password" mandatory="true" name="j_password" value="" tabindex="2">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td colspan="2">
                                                        <input type="submit" value="Login" name="login" tabindex="3" style="width: 87px;height:30px">
                                                    </td>
                                                </tr>
                                                <tr>

系统详情:

OpenSuse 11.4,Selenium 2.8.0,firefox 7.

提出的例外是:

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:96)
    at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:149)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:72)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:128)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:79)
    at com.beginning.jw.setUp(jw.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

3 个答案:

答案 0 :(得分:1)

我认为你应该使用网站的完整名称:

例如:

“http://www.site.com”

将此作为您的网址。

答案 1 :(得分:0)

在你提到的问题中,它打开网站并且无法登录,并且在你提到的评论中,“浏览器无法启动”。我有一点困惑 :) 如果我假设浏览器没有启动,那么听起来您需要在OpenSuse机器上更改IPv6设置。以下是常见问题的链接。

http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_WebDriver_fails_to_start_Firefox_on_Linux_due_to_java.net

答案 2 :(得分:0)

检查mozilla与硒版本的兼容性。