2011-06-13 27 views
13

Selenium如何在C#中使用Selenium?

我下载了C#客户端驱动程序和IDE。我设法记录了一些测试,并成功从IDE中运行它们。但是现在我想用C#来做到这一点。我将所有相关的DLL(Firefox)添加到项目中,但我没有Selenium类。一些你好的世界会很好。

+1

我想你已经看了吗? http://seleniumhq.org/docs/03_webdriver.html#the-5-minute-getting-started-guide – 2011-06-13 17:44:05

+0

有你甚至试着用搜索引擎?当然,有很多 - 也许是最重要的? - 与Java相关的Selenium东西,但是肯定还有基本的C#/ VS/Selenium教程,可以回答你的问题。 – 2015-04-24 13:57:14

+0

我下载了C#驱动程序和IDE - >你是什么意思的IDE?它用于编写C#硒代码?如Visual Studio。或者,它是Selenium IDE吗? – 2017-03-28 05:31:09

回答

26

Selenium Documentation

using OpenQA.Selenium.Firefox; 
using OpenQA.Selenium; 

class GoogleSuggest 
{ 
    static void Main(string[] args) 
    { 
     IWebDriver driver = new FirefoxDriver(); 

     //Notice navigation is slightly different than the Java version 
     //This is because 'get' is a keyword in C# 
     driver.Navigate().GoToUrl("http://www.google.com/"); 
     IWebElement query = driver.FindElement(By.Name("q")); 
     query.SendKeys("Cheese"); 
     System.Console.WriteLine("Page title is: " + driver.Title); 
     driver.Quit(); 
    } 
} 
3

要设置IDE中使用C#结合硒是使用Visual Studio速成。你可以将nUnit作为测试框架。以下链接为您提供更多细节。看来你已经设置了第一个链接中解释的内容。因此,检查的第二个链接,详细了解如何从上面的博客创建一个基本的脚本

How to setup C#,nUnit and selenium client drivers on VSExpress for Automated tests

Creating Basic Selenium web driver test case using Nunit and C#

示例代码

using System; 
    using Microsoft.VisualStudio.TestTools.UnitTesting; 
    //Step a 
    using OpenQA.Selenium; 
    using OpenQA.Selenium.Support; 
    using OpenQA.Selenium.Firefox; 
    using NUnit.Framework; 
    namespace NUnitSelenium 
    { 
[TestFixture] 
public class UnitTest1 
{  

    [SetUp] 
    public void SetupTest() 
    { 
    } 
    [Test] 
    public void Test_OpeningHomePage() 
    { 
     // Step b - Initiating webdriver 
     IWebDriver driver = new FirefoxDriver(); 
     //Step c : Making driver to navigate 
     driver.Navigate().GoToUrl("http://docs.seleniumhq.org/"); 

     //Step d 
     IWebElement myLink = driver.FindElement(By.LinkText("Download")); 
     myLink.Click(); 

     //Step e 
     driver.Quit(); 

     ) 
     } 
} 
4
  1. 安装的NuGet包管理
    下载链接:https://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
  2. 创建一个visual c#控制台应用程序
  3. 右键单击项目 - >管理Nuget包。
    搜索“硒”和安装包Selenium.Support

现在完成你准备写代码:)

对于代码IE下载即驾驶员
链接:http://selenium-release.storage.googleapis.com/index.html
开2.45,因为它是最新版本 下载IEDriverServer_x64_2.45.0.zip或IEDriverServer_Win32_2.45.0.zip
解压缩并将.exe文件简单地粘贴到任意位置,例如C:\
记住进一步使用的路径。

整体参考链接:http://www.joecolantonio.com/2012/07/31/getting-started-using-selenium-2-0-webdriver-for-ie-in-visual-studio-c/

我的示例代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Support.UI; 
using OpenQA.Selenium.IE; 

namespace Selenium_HelloWorld 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      IWebDriver driver = new InternetExplorerDriver("C:\\"); 
      driver.Navigate().GoToUrl("http://108.178.174.137"); 
      driver.Manage().Window.Maximize(); 
      driver.FindElement(By.Id("inputName")).SendKeys("apatra"); 
      driver.FindElement(By.Id("inputPassword")).SendKeys("asd"); 
      driver.FindElement(By.Name("DoLogin")).Click(); 

      string output = driver.FindElement(By.XPath(".//*[@id='tab-general']/div/div[2]/div[1]/div[2]/div/strong")).Text; 

      if (output != null ) 
      { 
       Console.WriteLine("Test Passed :) "); 
      } 
      else 
      { 
       Console.WriteLine("Test Failed"); 
      } 
     } 
    } 
} 
0

使用下面的代码,一旦你添加了所有必需的C#库在引用该项目。

using OpenQA.Selenium; 
using OpenQA.Selenium.Firefox; 
namespace SeleniumWithCsharp 
{ 
    class Test 
    { 
     public IWebDriver driver; 


     public void openGoogle() 
     { 
      // creating Browser Instance 
      driver = new FirefoxDriver(); 
      //Maximizing the Browser 
      driver.Manage().Window.Maximize(); 
      // Opening the URL 
      driver.Navigate().GoToUrl("http://google.com"); 
      driver.FindElement(By.Id("lst-ib")).SendKeys("Hello World"); 
      driver.FindElement(By.Name("btnG")).Click(); 
     } 

     static void Main() 
     { 
      Test test = new Test(); 
      test.openGoogle(); 
     } 

    } 
} 
2

我知道这是一个较老的问题,但我想我会把这些信息提供给其他人。

我很难找到的一件事是如何在C#中使用PageFactory。特别是对于多个IWebElements。如果你想使用PageFactory这里有几个例子。 Source: PageFactory.cs

要声明一个html WebElement,在类文件中使用它。

private const string _ID ="CommonIdinHTML"; 
[FindsBy(How = How.Id, Using = _ID)] 
private IList<IWebElement> _MultipleResultsByID; 

private const string _ID2 ="IdOfElement"; 
[FindsBy(How = How.Id, Using = _ID2)] 
private IWebElement _ResultById; 

不要忘了实例构造函数中的页面对象的元素。

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

现在,您可以在任何文件或方法中访问该元素。如果我们愿意,我们也可以从这些元素中获取相对路径。我更喜欢pagefactory因为:

  • 我也不需要直接调用使用driver.FindElement(By.Id( “ID”))
  • 的对象是lazy initialized

我司机用它来写我自己等待元素方法,WebElements包装器只访问我需要暴露给测试脚本的东西,并帮助保持事物的清洁。

这让生活变得更加简单,如果你有一个像数据列表动态(autogerated)webelements。您只需创建一个包装器,它将采用IWebElements并添加方法来查找您正在查找的元素。

1
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(@"D:\DownloadeSampleCode\WordpressAutomation\WordpressAutomation\Selenium", "geckodriver.exe"); 
service.Port = 64444; 
service.FirefoxBinaryPath = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"; 
Instance = new FirefoxDriver(service); 
+0

请解释你的答案。这里不鼓励使用代码解答,因为它们不会教任何东西 - 它们只是鼓励复制粘贴代码。您可以使用答案下方的编辑链接添加更多信息。谢谢! – 2016-09-19 23:05:17

1

C#

  1. 首先从Selenium IDE所有下载硒IDE的Firefox的。 使用和玩那么测试的情况下,记录的步骤,然后将其C#或Java项目导出按您的要求。

的代码文件中包含的代码是这样的:

using System; 
using System.IO; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Chrome; 
//add this name space to access WebDriverWait 
using OpenQA.Selenium.Support.UI; 
namespace MyTest 
{ 
[TestClass] 
public class MyTest 
{ 
    public static IWebDriver Driver = null; 

    // Use TestInitialize to run code before running each test 
    [TestInitialize()] 
    public void MyTestInitialize() 
    { 
     try 
     { 
      string path = Path.GetFullPath("");   //Copy the chrome driver to the debug folder in the bin or set path accordingly 
      Driver = new ChromeDriver(path); 
     } 
     catch (Exception ex) 
     { string error = ex.Message; } 
    } 

    // Use TestCleanup to run code after each test has run 
    [TestCleanup()] 
    public void MyCleanup() 
    { 
     Driver.Quit(); 
    } 

    [TestMethod] 
    public void MyTestMethod() 
    { 
     try 
     { 
      string url = "http://www.google.com"; 
      Driver.Navigate().GoToUrl(url); 

      IWait<IWebDriver> wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30.00));     // Waiter in Selenium 
      wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath(@"//*[@id='lst - ib']"))); 

      var txtBox = Driver.FindElement(By.XPath(@"//*[@id='lst - ib']")); 
      txtBox.SendKeys("Google Office"); 
      var btnSearch = Driver.FindElement(By.XPath("//*[@id='tsf']/div[2]/div[3]/center/input[1]")); 
      btnSearch.Click(); 

      System.Threading.Thread.Sleep(5000); 

     } 
     catch (Exception ex) 
     { 
      string error = ex.Message; 
     } 
    } 
} 
} 
  1. 你需要从here
  2. 你需要得到金块包硒的NuGet网站
  3. &必要的DLL得到铬司机
  4. 您需要从硒文档网站了解硒的基础

这一切......

1
using System; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using OpenQA.Selenium; 
using OpenQA.Selenium.Interactions; 
using OpenQA.Selenium.Support.UI; 
using SeleniumAutomationFramework.CommonMethods; 
using System.Text; 

[TestClass] 
    public class SampleInCSharp 
    { 

     public static IWebDriver driver = Browser.CreateWebDriver(BrowserType.chrome); 

     [TestMethod] 
     public void SampleMethodCSharp() 
     { 


      driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); 
      driver.Url = "http://www.store.demoqa.com"; 
      driver.Manage().Window.Maximize(); 

      driver.FindElement(By.XPath(".//*[@id='account']/a")).Click(); 
      driver.FindElement(By.Id("log")).SendKeys("kalyan"); 
      driver.FindElement(By.Id("pwd")).SendKeys("kalyan"); 
      driver.FindElement(By.Id("login")).Click(); 

      WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
      IWebElement myDynamicElement = wait.Until<IWebElement>(d => d.FindElement(By.LinkText("Log out"))); 

      Actions builder = new Actions(driver); 
      builder.MoveToElement(driver.FindElement(By.XPath(".//*[@id='menu-item-33']/a"))).Build().Perform(); 
      driver.FindElement(By.XPath(".//*[@id='menu-item-37']/a")).Click(); 
      driver.FindElement(By.ClassName("wpsc_buy_button")).Click(); 
      driver.FindElement(By.XPath(".//*[@id='fancy_notification_content']/a[1]")).Click(); 
      driver.FindElement(By.Name("quantity")).Clear(); 
      driver.FindElement(By.Name("quantity")).SendKeys("10"); 
      driver.FindElement(By.XPath("//*[@id='checkout_page_container']/div[1]/a/span")).Click(); 
      driver.FindElement(By.ClassName("account_icon")).Click(); 
      driver.FindElement(By.LinkText("Log out")).Click(); 
      driver.Close(); 
     } 
} 
相关问题