当我使用 Selenium 时未输入符号 (@)

At sign (@) is not being entered when I use Selenium

我一直在尝试练习 Selenium,由于电子邮件验证,我无法测试注册功能,但每次我 运行 测试时,它都会输入除“@”符号以外的所有内容,显然这是一个必需的字符。 这是网站,我一直在测试 -> http://a.testaddressbook.com/ (不好意思,如有不妥,我是第一次在这里发东西)

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

实际结果: 约洛test.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);
    }

}

我尝试了下面的代码,它工作正常。

@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);

}

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

解决之前,我:

  • 我使用了 Linux VM 并使用了相同的代码。我发现我的代码确实有效。所以,我开始认为我电脑上的某些东西不允许“@”被 Selenium 写入。
  • uninstalled/re-installed 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吧! (然后尝试您的原始代码)