【问题标题】:Creating a selenium test method that return the test results -java-创建一个返回测试结果的 selenium 测试方法 -java-
【发布时间】:2021-03-26 14:29:08
【问题描述】:

我是 selenium 的新手,我使用 java 和 IDE eclipse 来使用它登录测试有用的教程在这里:https://www.lambdatest.com/blog/selenium-java-tutorial-how-to-test-login-process/

所以我使用本教程创建了这个 testng 类

import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class testauthantification {


    public RemoteWebDriver driver = null;
    public String url = "https://www.lambdatest.com/";
    public static final String  username= "username of lamdatest";
    public static final String auth_key = "zUymhCJ2sIJWHyIsyuV9mXNiwGh5uYwqdmrNoHWVTSW5bN5VG9";
    public static final String URL = "@hub.lambdatest.com/wd/hub";
    boolean status = false;
    private String msg;

    
    

    @Test
    
    public void login () {
        // TODO Auto-generated method stub
        try {

            driver.manage().window().maximize();
            driver.get("https://profile.oracle.com/myprofile/account/secure/update-account.jspx?nexturl=https%3A//www.oracle.com");
     
            WebElement username = driver.findElement(By.id("sso_username"));
            WebElement password = driver.findElement(By.id("ssopassword"));
            WebElement login = driver.findElement(By.id("signin_button"));

            username.sendKeys("*******@gmail.com");
            password.sendKeys("the password");
            login.click();

            String actualUrl = "https://profile.oracle.com/myprofile/account/secure/update-account.jspx?nexturl=https%3A//www.oracle.com";
            String expectedUrl = driver.getCurrentUrl();

            if (actualUrl.equalsIgnoreCase(expectedUrl)) {
                
                 this.msg= "Test passed";
                System.out.println(msg);
                status = true; //Lambda status will be reflected as passed
              } else {
                System.out.println("Test failed"); //Lambda status will be reflected as passed

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            tearDown();
        }


    }
    



    @BeforeClass
    public void setUp() {
        DesiredCapabilities capabilities = new DesiredCapabilities();
    
        capabilities.setCapability("build", "your build name");
        capabilities.setCapability("name", "your test name");
        capabilities.setCapability("platform", "Windows 10");
        capabilities.setCapability("browserName", "Chrome");
        
        capabilities.setCapability("version","89.0");
        try {

            driver = new RemoteWebDriver(new URL("https://" + username + ":" + auth_key + URL), capabilities);

        } catch (Exception e) {

            System.out.println("Invalid grid URL" + e.getMessage());
        }

    }
    private void tearDown () {
        if (driver != null) {
            ((JavascriptExecutor) driver).executeScript("lambda-status=" + status); //Lambda status will be reflected as either passed/ failed

            driver.quit();

            System.out.println("The setup process is completed");
            System.out.println(msg);

        }
    }
    
}

在此示例中,我测试了对我的 oracle 帐户的身份验证,它运行良好并给了我这个

    [RemoteTestNG] detected TestNG version 7.3.0
    mars 26, 2021 3:02:02 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: W3C
    Test passed
    The setup process is completed
    Test passed
    PASSED: login
    
    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
    
    ===============================================

Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

但这里的问题是每次我想测试一个应用程序时,我都应该再次编写一个新类,但不同之处在于参数(用户名密码按钮),如果你只测试 4 个应用程序或少,但如果你有超过 10 个呢? 所以我想问的是一个方法,我们可以创建并给它 3 个元素,然后返回一个字符串 msg,测试通过或失败,例如

例如string testautantification(Element username, Element password, Element button)

(这里这个类元素包含我们将在 sendkey 中使用的 IDSELECTOR 及其值)

不仅如此,我们还可以添加 lambdatest 参数和令牌 如果有人可以帮助我,我希望我的问题很清楚。

【问题讨论】:

    标签: java selenium testing


    【解决方案1】:

    您仍然可以使用 TestNG 中的 @DataProvider 和您的测试 login() 并将所有参数放入其中。您可以根据需要传递任意数量的参数。对于下面的示例,您的“登录”测试将使用两组不同的参数运行两次。

    @DataProvider
    public Object[][] getDataProvider() {
        return new Object[][] {
                { "Username1_Id", "Password1_Id","LoginButton1_Id" },
                { "Username2_Id", "Password2_Id","LoginButton2_Id" },
        };
     }
    
    @Test(dataProvider = "getDataProvider")
    public void login(String usernameId, String passwordId, String loginButtonId) {
                 // TODO Auto-generated method stub
        try {
    
            driver.manage().window().maximize();
            driver.get("https://profile.oracle.com/myprofile/account/secure/update-account.jspx?nexturl=https%3A//www.oracle.com");
     
            WebElement username = driver.findElement(By.id(usernameId));
            WebElement password = driver.findElement(By.id(passwordId));
            WebElement login = driver.findElement(By.id(loginButtonId));
            //your test code
    }
    

    【讨论】:

    • 如果有用,请点击投票箭头下方的复选标记将答案标记为已接受
    • srry 但这个答案不是我的问题的正确答案,因为我需要的是一个函数,每次我需要它时我都可以使用给定的参数调用它,而无需填充 dataprovider 以及我确实解决了我的问题,但使用的是 Chrome 驱动程序(感谢您预留一些时间来验证我的问题)
    猜你喜欢
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多