Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing 如何在selenium中包含多个测试_Testing_Selenium_Selenium Grid - Fatal编程技术网

Testing 如何在selenium中包含多个测试

Testing 如何在selenium中包含多个测试,testing,selenium,selenium-grid,Testing,Selenium,Selenium Grid,我正在尝试创建一些测试用例,比如说有一个登录页面,现在我已经有了测试用例,一旦用户登录,我想使用Selenium运行几个其他测试。你知道我该怎么做吗?我看过Selenium网格,但它只是用于跨多个环境进行测试 谢谢 package org.openqa.selenium.example; import org.openqa.selenium.WebDriver; import org.openqa.selenium.By; import org.openqa.selenium.WebElemen

我正在尝试创建一些测试用例,比如说有一个登录页面,现在我已经有了测试用例,一旦用户登录,我想使用Selenium运行几个其他测试。你知道我该怎么做吗?我看过Selenium网格,但它只是用于跨多个环境进行测试

谢谢

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

public class LoginPage {
    private final WebDriver driver;

    public LoginPage(WebDriver driver) {
        this.driver = driver;

        // Check that we're on the right page.
        if (!"Outreach Configuration".equals(driver.getTitle())) {
            // Alternatively, we could navigate to the login page, perhaps logging out first
            throw new IllegalStateException("This is not the login page");
        }
    }

    // The login page contains several HTML elements that will be represented as WebElements.
    // The locators for these elements should only be defined once.

    //    By usernameLocator = By.name("username");
    //    By passwordLocator = By.name("password");
        By loginButtonLocator = By.name("submit");

    // The login page allows the user to type their username into the username field
    public LoginPage typeUsername(String username) {
        // This is the only place that "knows" how to enter a username
        driver.findElement(By.name("username")).sendKeys(username);

        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;    
    }

    // The login page allows the user to type their password into the password field
    public LoginPage typePassword(String password) {
        // This is the only place that "knows" how to enter a password
        //driver.findElement(passwordLocator).sendKeys(password);
        driver.findElement(By.name("password")).sendKeys(password);
        // Return the current page object as this action doesn't navigate to a page represented by another PageObject
        return this;    
    }

    // The login page allows the user to submit the login form
    public HomePage submitLogin() {
        // This is the only place that submits the login form and expects the destination to be the home page.
        // A seperate method should be created for the instance of clicking login whilst expecting a login failure. 
       driver.findElement(By.name("submit")).submit();

        // Return a new page object representing the destination. Should the login page ever
        // go somewhere else (for example, a legal disclaimer) then changing the method signature
        // for this method will mean that all tests that rely on this behaviour won't compile.
        return new HomePage(driver);    
    }

    // The login page allows the user to submit the login form knowing that an invalid username and / or password were entered
    public LoginPage submitLoginExpectingFailure() {
        // This is the only place that submits the login form and expects the destination to be the login page due to login failure.
    //    driver.findElement(By.name("submit")).submit();

        // Return a new page object representing the destination. Should the user ever be navigated to the home page after submiting a login with credentials 
        // expected to fail login, the script will fail when it attempts to instantiate the LoginPage PageObject.
        return new LoginPage(driver);   
    }

    // Conceptually, the login page offers the user the service of being able to "log into"
    // the application using a user name and password. 
    public HomePage loginAs(String username, String password) {
        // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here.
        typeUsername(username);
        typePassword(password);
        return submitLogin();
    }
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();

        // And now use this to visit Google
        driver.get("");
        LoginPage login = new LoginPage(driver);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HomePage a=login.loginAs("username","password");
        System.out.println(a);
        //driver.findElement(By.className("newbutton")).click();


    }
} 

您使用什么来“控制selenium webdriver”?JAVA朱尼特?还有别的吗?你能展示一些现有测试的代码吗?只是用代码更新了问题。现在假设我还想在用户登录后包含一个测试用例。如何做到这一点?将此代码转换为JUnit测试(可能是多个测试)将使您的生活更加轻松。一旦您进行了这些测试,就很容易在多个测试用例中调用
loginAs
,并在执行登录方法后检查其他条件。Selenium Grid确实解决了另一个问题,不需要它来解决您的问题。您将使用Selenium来解决它。JUnit是一个测试框架,它将帮助您构建各种测试,这些测试将使用selenium。请读一些像