【发布时间】: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 参数和令牌 如果有人可以帮助我,我希望我的问题很清楚。
【问题讨论】: