使用 selenium 的登录页面测试用例;Java 和 Eclipse IDE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22607887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:49:37  来源:igfitidea点击:

test cases for login page using selenium ; java and Eclipse IDE

javaeclipseseleniumjunit

提问by Samreen Adil

I am new to selenium webdriver, java (junit) and eclipse IDE.

我是 selenium webdriver、java (junit) 和 eclipse IDE 的新手。

Please help me to provide all the test cases for the login page.

请帮我提供登录页面的所有测试用例。

I have managed to write one test case in the test suite in eclipse IDE using selenium and Junit.

我已经设法使用 selenium 和 Junit 在 Eclipse IDE 的测试套件中编写了一个测试用例。

for your reference the two classes are:

供您参考,这两个类是:

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;  
import junit.textui.TestRunner;

public class TestSuite1 extends TestCase {    
    public static Test suite() {  
        TestSuite suite = new TestSuite();  

        suite.addTestSuite(TestCase1.class);
        //suite.addTestSuite((Case1) Testcase1.newInstance());  
        //suite.addTestSuite(TestCase1.newInstance());             
        return suite;  
    }  

    public static void main(String arg[]) {
        TestRunner.run(suite());
    }
}


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;

import com.thoughtworks.selenium.SeleneseTestCase;

public class TestCase1 extends SeleneseTestCase {
    public void setUp() throws Exception {  
        login();
    }

    public void login() {
        WebDriver driver = new FirefoxDriver();
        driver.get("http://");
        WebElement id = driver.findElement(By.name("username"));
        WebElement pass = driver.findElement(By.name("password"));
        WebElement button = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/form/p[3]/input"));         

        id.sendKeys("[email protected]");
        pass.sendKeys("abc123");
        button.submit();
    }
}

回答by ddavison

Try using button.click()instead of button.submit(). I've seen some issues using submit. Furthermore, if you are getting into selenium webdriver using eclipse, check out the Conductorframework. It simplifies things greatly. Your test would look like:

尝试使用button.click()代替button.submit(). 我在使用提交时看到了一些问题。此外,如果您正在使用 eclipse 进入 selenium webdriver,请查看Conductor框架。它大大简化了事情。你的测试看起来像:

@Config(url="http://mypage/login", browser=Browser.FIREFOX)
public class TestCase1 extends Locomotive {
    @Test
    public void login() {
        setText(By.name("username"), "[email protected]")
        .setText(By.name("password"), "abc123")
        .click(By.xpath("/html/body/div/div/div[2]/div/form/p[3]/input"))

        .validateTextPresent("You are now logged in");
    }
}