使用C#检查使用WebDriver登录gmail

时间:2012-08-10 14:58:28

标签: c# selenium gmail webdriver

我正在使用带有C#的WebDriver来自动测试gmail。我填写表格,然后点击按钮进入。如何验证登录的通过或失败?

[FindsBy(How = How.Id, Using = "Email")]
public IWebElement username;

[FindsBy(How = How.Id, Using = "Passwd")]
public IWebElement password;

[FindsBy(How = How.Id, Using = "signIn")]
public IWebElement loginButton;

public LoginForm()
{
    PageFactory.InitElements(Driver.driver, this);
}

public GmailPage Login(String username, String password)
{
    this.username.SendKeys(username);
    this.password.SendKeys(password);
    this.loginButton.Click();
    GmailPage result = new GmailPage();
    return result;
}

3 个答案:

答案 0 :(得分:0)

我会使用Nunit,因为你使用C#并查看Assertion docs:NUNIT

我会断言登录成功后在页面上显示的独特内容或断言您没有看到gmail要求重新输入凭据。

Assert.IsTrue( true, "your name here" );

Assert.False( false, "The username or password you entered is incorrect" );

答案 1 :(得分:0)

当用户名或密码错误时,单击gmail中的登录按钮后会显示一条消息。因此,您可以将文本验证为:

assertTrue(driver.getPageSource().contains("The username or password you entered is incorrect."), "Username or password is not correct");

如果您能够成功登录,则可以执行以下声明:

assertFalse(driver.getPageSource().contains("The username or password you entered is incorrect."), "some message");

以上示例基于使用TestNG作为框架的WebDriver(Selenium 2)。 assertTrue包含2个参数:i)布尔ii)消息。您可以使用NUnit考虑相同的概念。

上面的代码类似于Selenium RC的isTextPresent()

答案 2 :(得分:0)

尝试下面的代码,它对我有用,我通过使用chrome检查字段来获得它,因此右键单击该字段并复制XPath字段并在selenium中使用它们

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20)); 
driver.FindElement(By.XPath("//div[@class='sign-in-dialog-provider']")).Click(); 
driver.FindElement(By.Id("identifierId")).SendKeys("YOUR Email\n");

Thread.Sleep(10);


driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));

Actions builder = new Actions(driver);

builder.MoveToElement(driver.FindElement(By.XPath("//*[@id='password']/div[1]/div/div[1]/input"))).Build().Perform();

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

IWebElement checkOut = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='password']/div[1]/div/div[1]/input")));

checkOut.SendKeys("Your Password");

driver.FindElement(By.XPath("//*[@id='passwordNext']/content")).Click();
相关问题