如果您刚开始进行产品的Selenium自动化测试,则可能要自动化的第一页是“注册”或“登录”页面。如果您拥有像电子商务公司或“软件即服务”产品这样的在线平台,则“注册”页面将成为欢迎您的Web应用程序访问者的门户。它是平台上最简单但最重要的页面之一,并且出现在您可能要测试的每一个可能的用户旅程的开始。因此,它也是Web应用程序最重要的网页之一。
因此,今天,我们将更深入地研究如何执行 Signup表单页面的Selenium自动化测试。我们将使用Selenium和Java执行自动化测试。在我们以前的Selenium Java教程之一中,我们演示了如何使用Selenium自动化测试来自动化登录过程。
今天,我们将以LambdaTest的注册页面/注册页面为例。我们将以LambdaTest注册页面的测试用例和过程开始讨论,然后转到自动化部分,以了解如何增加浏览器的覆盖范围以进行有效的跨浏览器测试,并通过在Selenium Grid上进行并行测试来加速自动化测试。
在继续之前,让我们更详细地研究工作流程,并查看LambdaTest的 “ 注册”页面如何工作。
第一步,我们在任何浏览器中输入URL https://www.lambdatest.com,以访问LambdaTest网站的登录页面。您可以将以下屏幕截图用于登录页面。
从那里,我们将单击“免费注册”按钮,然后转到LambdaTest 的“ 注册”页面。
现在,让我们检查“注册”页面上的“功能”字段并将其列出。
- 组织 / 公司名称(文本字段)
- 用户的全名(文本字段)
- 工作电子邮件 ID(文本字段)
- 所需密码(文本字段)
- 用户的电话号码(文本字段)
- 条款和隐私政策(复选框)
- 免费注册(按钮)
注册页面的工作流程
输入所有必填字段:全名,工作电子邮件,所需密码,电话号码,条款和隐私权政策复选框。
验证字段:
- 工作电子邮件:不应是现有用户,有效的电子邮件。
- 密码:应提供密码所需的最小长度。
- 电话号码
- 条款和隐私政策复选框
到目前为止,我们已经讨论了有关注册页面的所有基本内容,特别是与LambdaTest的注册页面相关的内容。因此,让我们继续为该页面编写测试用例。
注册页面的可能测试方案
元素
通过单击所有按钮和复选框,检查页面上是否存在所有按钮,标题和复选框,以及它们是否正常工作。如果使用LambdaTest注册页面,则应选中以下按钮,文本字段,条款和隐私政策复选框等。
链接
检查页面上存在的所有链接是否都重定向到预期的页面。
我们还需要检查是否:
- LambdaTest链接重定向到LambdaTest正在启动正确的页面。
- 服务条款链接会打开一个新标签,其中包含重定向到服务条款页面。
- 隐私权政策链接会打开一个新的标签,重定向到隐私权政策页面。
- 登录链接从注册页面重定向到登录页面。
选填栏位
对于LambdaTest注册页面,组织或公司名称是唯一可选的。因此,我们将尝试在不提供组织或公司名称的情况下进行注册,并且在两种情况下都应能够完成注册(考虑到用户已正确提供了所有其他信息)。
必填项
对于LambdaTest注册页面,我们有以下必填字段,因此让我们讨论与每个字段相关的案例。
全名
→在不提供全名的情况下进行验证,它应该给出相应的错误,并且无法继续进行注册页面。
→通过提供全名进行验证,应该可以完成注册(考虑到用户已正确提供了所有其他信息)。
工作电邮
→使用有效的电子邮件ID进行验证,但在LambdaTest平台上具有该电子邮件ID的现有帐户,那么在这种情况下,不应允许该用户使用该电子邮件ID进行注册,并应提供相应的错误消息(考虑到用户已提供了所有其他信息信息正确)。
→在“工作电子邮件”字段中未提供任何详细信息的情况下进行验证,则应在没有任何电子邮件ID的情况下进行注册,并提供相应的错误消息。
→使用有效的电子邮件ID进行验证,该电子邮件ID在LambdaTest平台上没有任何具有该电子邮件ID的帐户,因此用户应能够继续进行并成功注册(考虑到用户已正确提供了所有其他信息)。
所需密码
→在“所需密码”字段中未提供任何密码的情况下进行验证,则应提供受尊重的错误消息以提供密码。
→如我们所见,密码必须至少包含字符长度,至少需要8个字符。因此,我们可以使用测试设计技术边界值分析(BVA),因为我们有8个字符的最低标准,因此我们可以使用7、8和9个字符的长度进行测试。
- 当它小于7时,它将错误提示给用户,用户可以继续完成注册过程。
- 如果是8,则用户应该可以成功完成注册过程,而不会出现任何错误。(考虑到用户已正确提供了所有其他信息)。
- 如果为9,那么用户也应该可以成功完成注册过程而不会出现任何错误。(考虑到用户已正确提供了所有其他信息)。
电话
→验证时不要在该字段中提供任何电话号码,因为这也是必填字段,因此用户在不提供电话号码的情况下不应继续进行并完成注册。
→验证提供的无效电话号码(2-3位电话号码),在这种情况下,用户也将无法继续使用无效的提供电话号码完成注册。
→验证并在字段中提供有效的电话号码,在这种情况下,用户应能够使用有效的电话号码成功注册(考虑到用户已正确提供了所有其他信息)。
服务条款复选框:
→验证而不选中服务条款复选框,因为注册过程中的必填字段用户如果不选中此复选框就无法继续完成注册过程。
→选中使用服务条款验证复选框,然后用户可以成功完成注册而不会出现任何错误(考虑到用户已正确提供了所有其他信息)。
所以我们到目前为止已经看到
- 通常在“注册”页面上显示哪些字段,以及LambdaTest的注册页面示例。
- 注册页面的预期工作流程。
- “注册”页面的测试用例/方案
使用Selenium和Java自动执行用户注册表单
到目前为止,在Selenium Java教程中,您已经对需要在“注册”页面上进行测试的内容有了一定的了解。考虑到这一点,让我们通过使用Java和TestNG执行Selenium自动化测试,来付诸行动并使该“注册”页面自动化。
现在,我们将提出一个Selenium自动化脚本,并在在线Selenium Grid上执行它。我们还将利用并行测试的功能来快速跟踪自动化的跨浏览器测试。但在此之前,您应该已经准备好以下先决条件。
先决条件
在转向脚本之前,让我们先了解一些基本前提条件,以编写和执行Selenium Automation测试脚本。
- JDK和JRE –这是执行我们系统中的任何Java程序所必需的。您可以从Oracle的官方网站下载它。 https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html
- Selenium Jars –要编写任何Selenium自动化测试脚本,需要在项目中包含Selenium Jars。您可以从Selenium的官方网站下载Selenium Jars 。
- TestNG框架 –在以下测试脚本中,我们将TestNG框架用于Selenium自动化测试,这将有助于我们生成测试报告,确定测试用例的优先级,对测试用例进行分组以及在不同的浏览器上并行执行。您可以下载TestNG Jars,也可以使用TestNG Eclipse URL从Eclipse Marketplace将其直接添加到Eclipse中,也可以参考TestNG进行下载。
在下面的示例中,我们将测试脚本与跨浏览器测试工具LambdaTest集成在一起。想知道为什么我们要在基于云的硒测试工具(如LambdaTest)上运行硒自动化测试?
虽然,Selenium使您能够跨不同的浏览器自动化Web应用程序测试。您只能在运行本地Webdriver的计算机上安装的浏览器上执行Selenium自动化测试。现在,安装数百种浏览器并维护内部Selenium基础结构的库可能非常耗时,而且也很耗时。
这就是为什么建议在在线Selenium测试工具(例如LambdaTest)上运行Selenium自动化测试的原因,但这不是唯一的原因。LambdaTest还有很多!
什么是LambdaTest?
LambdaTest是云上的跨浏览器测试工具,可在包含2000多个云上真实浏览器的Selenium Grid上提供手动和自动浏览器兼容性测试。LambdaTest允许您执行并行测试,生成Selenium自动化测试的详细测试报告,为您提供整个测试脚本执行的录制视频,以及将Selenium自动化测试套件与您喜欢的CI / CD工具无缝集成的功能。它还为Jenkins等提供了本机插件。
使用LambdaTest对注册页面进行Selenium自动化测试
因此,现在让我们讨论如何将测试脚本与LambdaTest集成并执行以查看结果的过程。在硒测试脚本中,我们将WebDriver初始化为:
WebDriver driver = new ChromeDriver();
Or
WebDriver driver = new FirefoxDriver();
Or
WebDriver driver = new EdgeDriver();
首先,当我们想在LambdaTest Selenium网格上运行测试脚本时,我们必须将WebDriver初始化为
WebDriver driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + "@hub.lambdatest.com/wd/hub"),
DesiredCapabilities.firefox());
现在将出现一些问题,例如这些用户名和访问密钥是做什么用的?因此,我们可以从LambdaTest Capabilities Generator中生成这些用户名和访问密钥。
在这里,一旦我们选择了操作系统,浏览器,Selenium WebDriver版本等,我们就可以获取我们可以在测试脚本中使用的用户名和访问密钥。
注意:要生成此用户名和访问密钥,应先注册并登录LambdaTest帐户。
因此,在生成这些用户名和访问密钥之后,我们可以在测试脚本中与LambdaTest集成使用它们。我们已经为LambdaTest注册页面/注册页面创建了以下测试脚本,我们已经在上面讨论了作为手动测试用例和场景的测试场景。
在执行任何自动化测试用例时,在测试套件开始时很少执行一些常见的步骤,而在每个测试用例之前都需要执行的步骤很少,我们可以将其称为测试用例的前提条件。
例如:在执行每个测试用例时,我们需要先打开浏览器并导航到目标URL,然后再开始执行实际的测试用例,删除cookie等。
为此,我们使用来自Test NG的@BeforeClass和@BeforeMethod批注,因此在我们的情况下,我们将需要
- 设置一些功能(浏览器,平台等)
- 开启浏览器
- 删除cookies
- 打开LambdaTest平台并导航到“注册”页面
public String username = "yadneshshah91"; //your LambdaTest username
public String accesskey = "1234ABCD"; //your LambdaTest access key
public static RemoteWebDriver driver = null;
public String gridURL = "@hub.lambdatest.com/wd/hub"; //LambdaTest Hub URL
boolean status = false;
//Setting up capabilities to run our test script
@Parameters(value= {"browser","version"})
@BeforeClass
public void setUp(String browser, String version) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", browser);
capabilities.setCapability("version", version);
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
capabilities.setCapability("build", "LambdaTestSampleApp");
capabilities.setCapability("name", "LambdaTestJavaSample");
capabilities.setCapability("network", true); // To enable network logs
capabilities.setCapability("visual", true); // To enable step by step screenshot
capabilities.setCapability("video", true); // To enable video recording
capabilities.setCapability("console", true); // To capture console logs
try {
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
//Opening browser with the given URL and navigate to Registration Page
@BeforeMethod
public void openBrowser()
{
driver.manage().deleteAllCookies();
driver.get("https://www.lambdatest.com/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
WebElement signUpButton = driver.findElement(By.xpath("//a[contains(text(),'Free Signup')]"));
signUpButton.click();
}
在上面的代码部分中,请注意DesiredCapabilities对象。该对象用于设置浏览器,版本,操作系统等功能,并指示LambdaTest网格在所需的配置上运行测试。如果要在本地运行测试,则不一定需要此对象。,然后我们打开浏览器,删除cookie,导航到LambdaTest的默认页面,然后单击“注册”按钮以重定向到我们的目标页面,即“注册”页面。
在测试用例的最后或执行之后需要执行的操作/任务也是如此。
例如:关闭浏览器会话等。
为此,我们使用了TestNG中的@AfterMethod或@AfterMethod批注。如果您不熟悉TestNG,那么建议您阅读有关TestNG批注的详细文章。
// Closing the browser session after completing each test case
@AfterClass
public void tearDown() throws Exception {
if (driver != null) {
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}
}
在上面的代码部分中,您可以看到我们已经关闭了浏览器会话,每个测试案例之后我们都需要完成该工作。
正如我们所看到的,现在让我们在每个测试用例之前和之后进行什么工作以及如何工作,让我们针对上述测试用例执行Selenium自动化测试。在任何Selenium自动化测试脚本中,要对网页元素执行任何操作,我们需要首先找到它们。为此,我们使用了硒定位器。
深入测试用例
让我们研究一下测试用例,以理解我们需要自动进行Selenium测试的测试场景。
测试用例1
在“注册”页面上验证期望的元素是否可用。
//Verifying elements on Registration page
@Test
public void verifyElemntsOnPageTest()
{
WebElement lambdaTestLogo = driver.findElement(By.xpath("//p[@class='signup-titel']"));
lambdaTestLogo.isDisplayed();
WebElement signUpTitle = driver.findElement(By.xpath("//p[@class='signup-titel']"));
signUpTitle.isDisplayed();
WebElement termsText = driver.findElement(By.xpath("//label[@class='woo']"));
termsText.isDisplayed();
WebElement loginLinkText = driver.findElement(By.xpath("//p[@class='login-in-link test-left']"));
loginLinkText.isDisplayed();
}
在上述测试用例函数verifyElemntsOnPageTest中,我们找到了一些元素,例如LambdaTest徽标,注册标题,服务条款文本等。要定位Web元素,我们可以使用不同的定位策略,例如ID,XPath,CSS Selector,ClassName,在上述情况下,我们使用XPath找到了这些元素。
找到这些元素后,如果使用方法isDisplayed()显示了这些元素,我们将对其进行验证。
测试案例2
To verify the redirections with the links present on the pages.
//Verifying redirection to the terms and conditions page
@Test
public void termsRedirectionTest()
{
WebElement termsLink = driver.findElement(By.xpath("//a[contains(text(),'Terms')]"));
termsLink.click();
Set <String> allWindows = driver.getWindowHandles();
for(String handle : allWindows)
{
driver.switchTo().window(handle);
}
String expectedURL = "https://www.lambdatest.com/terms-of-service";
String actualURL = driver.getCurrentUrl();
//System.out.println(actualURL);
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Terms of Service - LambdaTest";
String actualTitle = driver.getTitle();
//System.out.println(actualTitle);
Assert.assertEquals(actualTitle, expectedTitle);
}
//Verifying Privacy policy page redirection
@Test
public void privacyPolicyRedirectionTest()
{
WebElement privacyPolicyLink = driver.findElement(By.xpath("//a[contains(text(),'Privacy')]"));
privacyPolicyLink.click();
Set <String> allWindows = driver.getWindowHandles();
for(String handle : allWindows)
{
driver.switchTo().window(handle);
}
String expectedURL = "https://www.lambdatest.com/privacy";
String actualURL = driver.getCurrentUrl();
//System.out.println(actualURL);
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Privacy Policy | LambdaTest";
String actualTitle = driver.getTitle();
//System.out.println(actualTitle);
Assert.assertEquals(actualTitle, expectedTitle);
}
//Verifying redirection to the Login page from Registration page
@Test
public void loginRedirectionTest()
{
WebElement loginLink = driver.findElement(By.xpath("//a[contains(text(),'Login')]"));
loginLink.click();
String expectedURL = "https://accounts.lambdatest.com/login";
String actualURL = driver.getCurrentUrl();
//System.out.println(actualURL);
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Login - LambdaTest";
String actualTitle = driver.getTitle();
//System.out.println(actualTitle);
Assert.assertEquals(actualTitle, expectedTitle);
}
//Verifying redirection to the landing page
@Test
public void landingPageRedirectionTest()
{
WebElement lambdaTestLogo = driver.findElement(By.xpath("//p[@class='logo-home']//a//img"));
lambdaTestLogo.click();
String expectedURL = "https://www.lambdatest.com/";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "";
}
因此,在这里,您可以看到我们为“注册”页面上存在的不同链接添加了4种方法,并且在单击这些链接后我们已经验证了用户是否被重定向到正确的URL和页面。在上述方法中,我们还添加了断言以验证重定向是否按预期完成。
测试案例3
通过提供有效的输入数据来验证注册过程。
// Registration with all valid data
@Test
public void validRegistrationTest(){
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedURL = "https://accounts.lambdatest.com/email/verify";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Verify Your Email Address - LambdaTest";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
测试案例4
用不同的无效输入数据验证注册过程。
// Registration without providing Company Name field
@Test
public void emptyCompanyNameTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
/*
* Set <String> allWindows = driver.getWindowHandles();
*
* for(String handle : allWindows) { driver.switchTo().window(handle); }
*/
String expectedURL = "https://accounts.lambdatest.com/email/verify";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Verify Your Email Address - LambdaTest";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
// Registration without providing Name field
@Test
public void emptyNameTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Please enter your Name";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Please enter your Name')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration without providing user email field
@Test
public void emptyEmailTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("test");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Please enter your Email Address";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Please enter your Email Address')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration with email id which already have account
@Test
public void invalidEmailTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "This email is already registered";
WebElement exp = driver.findElement(By.xpath("//p[@class='error-mass']"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration without providing password field
@Test
public void emptyPasswordTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Please enter a desired password";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Please enter a desired password')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration with invalid password
@Test
public void inValidPasswordTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("T");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Password should be at least 8 characters long";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Password should be at least 8 characters long')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
//Password should be at least 8 characters long
}
// Registration without providing user phone number field
@Test
public void emptyPhoneTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "The phone field is required.";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'The phone field is required.')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration with providing invalid user phone number field
@Test
public void inValidPhoneTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("98");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Please enter a valid Phone number";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Please enter a valid Phone number')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
//Please enter a valid Phone number
}
// Registration without accepting terms and condition tickbox
@Test
public void uncheckedTerms()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
//WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
//termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedTermsErrorMessage = "To proceed further you must agree to our Terms of Service and Privacy Policy";
WebElement uncheckedTermCheckbox = driver.findElement(By.xpath("//p[@class='error-mass mt-2']"));
String actualTermsErrorMessage = uncheckedTermCheckbox.getText();
//To proceed further you must agree to our Terms of Service and Privacy Policy
Assert.assertEquals(actualTermsErrorMessage, expectedTermsErrorMessage);
}
在上面的代码部分中,您可以看到我们涵盖了为一个或另一个字段提供无效输入数据的不同情况。同样,在每种方法的最后,我们都添加了断言以及针对我们提供的无效输入的预期错误消息。
使用Selenium和Java进行自动注册的整个测试脚本
正如我们看到的Selenium自动化测试脚本和点点滴滴的不同测试用例一样,现在,让我们结合此代码来呈现整个测试脚本,可以在作为参数提供的多个浏览器上执行该测试脚本。
因此,请同时参考最后提供的testng.xml文件,这对于在不同的浏览器中并行执行Selenium自动化测试非常重要。
LambdaTest注册页面的自动化测试脚本:
package com.lambdatest;
//TestNG Todo : Sample App
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import com.beust.jcommander.Parameter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class SignUpTest{
public String username = "yadneshshah91";
public String accesskey = "iD4oCZKyC8R2kwH1kLhTxAGgXZFANGoyzGDzY9RTqDTx9jskZo";
public static RemoteWebDriver driver = null;
public String gridURL = "@hub.lambdatest.com/wd/hub";
boolean status = false;
//Setting up capabilities to run our test script
@Parameters(value= {"browser","version"})
@BeforeClass
public void setUp(String browser, String version) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", browser);
capabilities.setCapability("version", version);
capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get any available one
capabilities.setCapability("build", "LambdaTestSampleApp");
capabilities.setCapability("name", "LambdaTestJavaSample");
capabilities.setCapability("network", true); // To enable network logs
capabilities.setCapability("visual", true); // To enable step by step screenshot
capabilities.setCapability("video", true); // To enable video recording
capabilities.setCapability("console", true); // To capture console logs
try {
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), capabilities);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
//Opening browser with the given URL and navigate to Registration Page
@BeforeMethod
public void openBrowser()
{
driver.manage().deleteAllCookies();
driver.get("https://www.lambdatest.com/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
WebElement signUpButton = driver.findElement(By.xpath("//a[contains(text(),'Start Free Testing')]"));
signUpButton.click();
}
//Verifying elements on Registration page
@Test
public void verifyElemntsOnPageTest()
{
WebElement lambdaTestLogo = driver.findElement(By.xpath("//p[@class='signup-titel']"));
lambdaTestLogo.isDisplayed();
WebElement signUpTitle = driver.findElement(By.xpath("//p[@class='signup-titel']"));
signUpTitle.isDisplayed();
WebElement termsText = driver.findElement(By.xpath("//label[@class='woo']"));
termsText.isDisplayed();
WebElement loginLinkText = driver.findElement(By.xpath("//p[@class='login-in-link test-left']"));
loginLinkText.isDisplayed();
}
//Verifying redirection to the terms and conditions page
@Test
public void termsRedirectionTest()
{
WebElement termsLink = driver.findElement(By.xpath("//a[contains(text(),'Terms')]"));
termsLink.click();
Set <String> allWindows = driver.getWindowHandles();
for(String handle : allWindows)
{
driver.switchTo().window(handle);
}
String expectedURL = "https://www.lambdatest.com/terms-of-service";
String actualURL = driver.getCurrentUrl();
//System.out.println(actualURL);
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Terms of Service - LambdaTest";
String actualTitle = driver.getTitle();
//System.out.println(actualTitle);
Assert.assertEquals(actualTitle, expectedTitle);
}
//Verifying Privacy policy page redirection
@Test
public void privacyPolicyRedirectionTest()
{
WebElement privacyPolicyLink = driver.findElement(By.xpath("//a[contains(text(),'Privacy')]"));
privacyPolicyLink.click();
Set <String> allWindows = driver.getWindowHandles();
for(String handle : allWindows)
{
driver.switchTo().window(handle);
}
String expectedURL = "https://www.lambdatest.com/privacy";
String actualURL = driver.getCurrentUrl();
//System.out.println(actualURL);
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Privacy Policy | LambdaTest";
String actualTitle = driver.getTitle();
//System.out.println(actualTitle);
Assert.assertEquals(actualTitle, expectedTitle);
}
//Verifying redirection to the Login page from Registration page
@Test
public void loginRedirectionTest()
{
WebElement loginLink = driver.findElement(By.xpath("//a[contains(text(),'Login')]"));
loginLink.click();
String expectedURL = "https://accounts.lambdatest.com/login";
String actualURL = driver.getCurrentUrl();
//System.out.println(actualURL);
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Login - LambdaTest";
String actualTitle = driver.getTitle();
//System.out.println(actualTitle);
Assert.assertEquals(actualTitle, expectedTitle);
}
//Verifying redirection to the landing page
@Test
public void landingPageRedirectionTest()
{
WebElement lambdaTestLogo = driver.findElement(By.xpath("//p[@class='logo-home']//a//img"));
lambdaTestLogo.click();
String expectedURL = "https://www.lambdatest.com/";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(actualURL, expectedURL);
}
// Registration with all valid data
@Test
public void validRegistrationTest(){
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedURL = "https://accounts.lambdatest.com/email/verify";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Verify Your Email Address - LambdaTest";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
// Registration without providing Company Name field
@Test
public void emptyCompanyNameTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
/*
* Set <String> allWindows = driver.getWindowHandles();
*
* for(String handle : allWindows) { driver.switchTo().window(handle); }
*/
String expectedURL = "https://accounts.lambdatest.com/email/verify";
String actualURL = driver.getCurrentUrl();
Assert.assertEquals(actualURL, expectedURL);
String expectedTitle = "Verify Your Email Address - LambdaTest";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
// Registration without providing Name field
@Test
public void emptyNameTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Please enter your Name";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Please enter your Name')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration without providing user email field
@Test
public void emptyEmailTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("test");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Please enter your Email Address";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Please enter your Email Address')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration with email id which already have account
@Test
public void invalidEmailTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "This email is already registered";
WebElement exp = driver.findElement(By.xpath("//p[@class='error-mass']"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration without providing password field
@Test
public void emptyPasswordTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Please enter a desired password";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Please enter a desired password')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration with invalid password
@Test
public void inValidPasswordTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("T");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Password should be at least 8 characters long";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Password should be at least 8 characters long')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
//Password should be at least 8 characters long
}
// Registration without providing user phone number field
@Test
public void emptyPhoneTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "The phone field is required.";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'The phone field is required.')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
}
// Registration with providing invalid user phone number field
@Test
public void inValidPhoneTest()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("98");
WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedErrorMsg = "Please enter a valid Phone number";
WebElement exp = driver.findElement(By.xpath("//p[contains(text(),'Please enter a valid Phone number')]"));
String actualErrorMsg = exp.getText();
Assert.assertEquals(actualErrorMsg, expectedErrorMsg);
//Please enter a valid Phone number
}
// Registration without accepting terms and condition tickbox
@Test
public void uncheckedTerms()
{
WebElement companyName = driver.findElement(By.name("organization_name"));
companyName.sendKeys("TestCompany");
WebElement fullName = driver.findElement(By.name("name"));
fullName.sendKeys("TestName");
WebElement email = driver.findElement(By.name("email"));
email.sendKeys("[email protected]");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("[email protected]");
WebElement phone = driver.findElement(By.name("phone"));
phone.sendKeys("9876543210");
//WebElement termsOfServices = driver.findElement(By.id("terms_of_service"));
//termsOfServices.click();
WebElement signUp = driver.findElement(By.xpath("//button[contains(@class,'btn sign-up-btn-2 btn-block')]"));
signUp.click();
String expectedTermsErrorMessage = "To proceed further you must agree to our Terms of Service and Privacy Policy";
WebElement uncheckedTermCheckbox = driver.findElement(By.xpath("//p[@class='error-mass mt-2']"));
String actualTermsErrorMessage = uncheckedTermCheckbox.getText();
//To proceed further you must agree to our Terms of Service and Privacy Policy
Assert.assertEquals(actualTermsErrorMessage, expectedTermsErrorMessage);
}
// Closing the browser session after completing each test case
@AfterClass
public void tearDown() throws Exception {
if (driver != null) {
((JavascriptExecutor) driver).executeScript("lambda-status=" + status);
driver.quit();
}
}
}
在上面的测试脚本示例中,您可以观察到我们使用了参数化选项来提供浏览器名称和浏览器版本,以便我们可以根据需要对上述测试脚本进行测试的要求来提供浏览器和浏览器版本。我们还可以提供多种浏览器和版本来并行执行测试脚本。
为此,我们需要提供testng.xml文件中的浏览器名称和版本的这些值,我们将其与每个TestNG脚本一起使用。
例如,您可以参考以下testng.xml,其中我们提供了2个浏览器(Chrome和Firefox)及其各自的版本。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="3" name="LambaTestSuite" parallel="tests">
<test name="FirefoxTest">
<parameter name="browser" value="firefox"/>
<parameter name="version" value="69.0"/>
<classes>
<class name="com.lambdatest.SignUpTest"/>
</classes>
</test> <!-- Test -->
<test name="ChromeTest">
<parameter name="browser" value="chrome"/>
<parameter name="version" value="77.0"/>
<classes>
<class name="com.lambdatest.SignUpTest"/>
</classes>
</test> <!-- Test -->
<test name="EdgeTest">
<parameter name="browser" value="edge"/>
<parameter name="version" value="44.0"/>
<classes>
<class name="com.lambdatest.SignUpTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
以下是通过LambdaTest平台触发的最终执行的快照。我们已经在两个浏览器(即Google Chrome 77和Mozilla Firefox 69)上执行了跨浏览器测试自动化脚本。
如果凝视LambdaTest 自动化仪表板,您将能够注意到多个选项卡,它们代表测试自动化脚本的不同类型的日志。您可以使用标签来分析命令日志,网络日志,原始Selenium日志,元数据等。您还可以在LambdaTest云服务器上获得跨浏览器测试脚本的完整视频记录,以及逐个命令的屏幕截图。
这就是全部了
现在,您可以使用Selenium和Java来实现“注册”页面的自动化。在此Selenium Java教程中,我们着眼于LambdaTest的注册过程。我们考虑了不同的测试用例,编译了一个测试脚本以执行它们,然后分析了Selenium Automation测试脚本以在不同的浏览器上并行运行。
在TestNG框架的帮助下,使用Selenium和Java执行Selenium自动化测试脚本。如果您觉得我错过了有关LambdaTest注册页面的特殊测试用例,请随时告诉我。洗耳恭听!测试愉快。🙂