使用硒时未输入符号(@)

时间:2018-09-05 08:21:18

标签: selenium automated-tests email-validation

我一直在尝试练习Selenium,由于电子邮件验证,我无法测试注册功能,但是每次运行测试时,它都会输入除“ @”符号之外的所有内容,并且显然这是必填字符。 这是我一直在测试的网站-> http://a.testaddressbook.com/ (对不起,如果搞砸了,这是我第一次在这里发布内容)

预期结果: yolo@test.com(将显示在电子邮件输入字段中)

实际结果: yolotest.com without "@"

步骤定义文件(它的重要部分): StepDef.java

以及引用步骤定义文件的页面: SignUp.java

编辑:我从Eclipse开始,然后更改为IntelliJ(希望这只是一个IDE设置问题,但这不是-值得一试)。 我还尝试添加“ @”符号的unicode,但仍然没有键入“ @”。

根据要求,您可以在下面找到代码片段: SignUp.java(页面)

package Pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;

public class SignUp {

    @FindBy(id = "user_email")
    private WebElement userName;

    @FindBy(id = "user_password")
    private WebElement userPass;


    public void credentials(WebDriver driver) {
        Actions cred = new Actions(driver);
        cred.click(userName).sendKeys("yolo@test.com").perform();
        cred.click(userPass).sendKeys("Batman").perform();
    }
}

StepDef.java(步骤定义文件)

@Then("^I fill out fields with information$")
public void i_fill_out_fields_with_information() throws Throwable {
    SignUp filldetails = PageFactory.initElements(driver, SignUp.class);
    filldetails.credentials(driver);
    Thread.sleep(2000);
}

编辑(建议代码):

package com.qa.quickstart.Bookthing;

import static org.junit.Assert.*;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class TestThing {


    ChromeDriver driver;
    String url="http://a.testaddressbook.com/sign_in";


    @Test
    public void test() throws InterruptedException {
         System.setProperty("webdriver.chrome.driver", "C:\\Users\\Laptop\\eclipse-workspace\\chromedriver.exe");
         driver = new ChromeDriver();
         driver.manage().window().maximize();
         driver.get(url);


         WebDriverWait wait = new WebDriverWait(driver, 50);
         driver.findElement(By.linkText("Sign up")).click();
         WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@value='Sign up']")));
         WebElement email = wait.until(ExpectedConditions.elementToBeClickable(By.id("user_email")));
         email.sendKeys("apple@test.com");
         WebElement pass = wait.until(ExpectedConditions.elementToBeClickable(By.id("user_password")));
         pass.sendKeys("banana");

         Thread.sleep(15000);
    }

}

2 个答案:

答案 0 :(得分:0)

我尝试了下面的代码,效果很好。

@Test(enabled=true)
public void loginTry() throws InterruptedException {
    driver.get("http://a.testaddressbook.com/sign_in");
    WebDriverWait wait = new WebDriverWait(driver, 50);
    wait.until(ExpectedConditions.elementToBeClickable(By.id("sign-in")));
    driver.findElement(By.linkText("Sign up")).click();
    WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@value='Sign up']")));
    WebElement email = wait.until(ExpectedConditions.elementToBeClickable(By.id("user_email")));
    email.sendKeys("yolo@test.com");
    WebElement pass = wait.until(ExpectedConditions.elementToBeClickable(By.id("user_password")));
    pass.sendKeys("somepassword");
    Thread.sleep(15000);

}

答案 1 :(得分:0)

有趣的是,我设法解决了这个问题!

在解决之前,我:

  • 我使用Linux VM并使用了相同的代码。我发现我的代码确实有效。因此,我开始认为我的计算机上不允许Selenium编写“ @”。
  • 未安装/重新安装的Google Chrome
  • 更改了键盘布局(英语是默认设置,但也可以选择匈牙利语布局,后来我决定重新安装它,解决方案对此未做任何更改,默认仍然是英文)

但是,显然解决方案是 Key.Chord ! 一个例子:

email.sendKeys(Keys.chord(Keys.ALT, "batman@msn.com"));

email.sendKeys(Keys.chord("robin@msn.com"));

它不是专业的,但仍然有效。我还发现,在此解决方案之后,我的原始代码起作用了。因此,我可以坚持下面的代码:

email.sendKeys("apple@test.com");

简而言之,如果您处于类似情况,则只需尝试 Key.Chord ! (然后尝试您的原始代码)