How do I use Selenium in C#?

后端 未结 9 925
孤街浪徒
孤街浪徒 2020-12-01 06:38

Selenium.

I downloaded the C# client drivers and the IDE. I managed to record some tests and successfully ran them from the IDE. But now I want to do that using C#. I

相关标签:
9条回答
  • 2020-12-01 07:37

    From the 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();
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:38
    1. You will need to install Microsoft Visual Studio community Edition
    2. Create a new project as Test Project of C#
    3. Add Selenium references from the NuGet Package Manager. Then you will be all set.
    4. Create a new class and use [Test Class] and [Test Method] annotations to run your script
    5. You can refer to Run Selenium C# | Setup Selenium and C# | Configure Selenium C# for more details.
    0 讨论(0)
  • 2020-12-01 07:39
    1. Install the NuGet packet manager

      Download link: https://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c

    2. Create a C# console application

    3. Right-click on the project → Manage NuGet Packages. Search for "Selenium" and install package Selenium.Support.

    You are done now, and you are ready to write your code :)

    For code with Internet Explorer, download the Internet Explorer driver.

    Link: http://selenium-release.storage.googleapis.com/index.html

    • Open 2.45 as its the latest release
    • Download IEDriverServer_x64_2.45.0.zip or IEDriverServer_Win32_2.45.0.zip
    • Extract and simply paste the .exe file at any location, for example C:\
    • Remember the path for further use.

    Overall reference link: Selenium 2.0 WebDriver with Visual Studio, C#, & IE – Getting Started

    My sample code:

    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 讨论(0)
提交回复
热议问题