【问题标题】:Run same script / test cases with different 10 different usernames, using selenium c#使用 selenium c# 使用不同的 10 个不同用户名运行相同的脚本/测试用例
【发布时间】:2020-08-28 09:04:43
【问题描述】:

请不要对问题给予否定,而是帮助我解决问题。 这是我使用用户名作为一个用户登录的代码,

        driver.Navigate().GoToUrl(URL);         
        driver.FindElement(By.ID("login")).Click();
        driver.FindElement(By.ID("user")).SendKeys(Username1);
        driver.FindElement(By.ID("pass")).SendKeys(password);
        driver.FindElement(By.ID("access")).Click();

如何使用不同的用户名依次运行相同的代码

  readonly static string Username1 = "User1"
  readonly static string Username2 = "User2"
  readonly static string Username3 = "User3"
  reasonly static string password =  "passqwert"   

一旦我登录,其余的测试用例就会被执行。测试用例不会改变任何用户只需要使用不同的用户名登录。

【问题讨论】:

  • Stackoverflow 并不是一个真正的教程网站,这正是您所要求的。处理变量的方法有很多种,因为这就是全部,这使您的问题非常广泛。您可以通过提供您的脚本并告诉我们您尝试了什么以及为什么它不起作用来稍微缩小范围。对于这个网站来说,这可能仍然过于宽泛,但你会更接近。除此之外,您可能应该查看 C# 教程以掌握基础知识。
  • 谢谢,@TylerMacMillan 我将编辑问题并添加代码。

标签: c# selenium testing selenium-webdriver


【解决方案1】:

听起来您需要利用您正在使用的单元测试框架。因此,例如,如果您使用 MsTest,您可以像这样使用 DataRow 属性...

  readonly static string password = "passqwert";   

  [DataTestMethod]
  [DataRow("User1")]
  [DataRow("User2")]
  [DataRow("User3")]
  public void TheTest(string username)
  {
        ...
        driver.Navigate().GoToUrl(URL);         
        driver.FindElement(By.ID("login")).Click();
        driver.FindElement(By.ID("user")).SendKeys(username);
        driver.FindElement(By.ID("pass")).SendKeys(password);
        driver.FindElement(By.ID("access")).Click();
  }

如果使用 NUnit 或 xUnit 等其他测试框架,也可以实现类似的效果。

【讨论】:

  • 感谢您的回复给了我一些尝试的想法谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多