How do I use Selenium in C#?

后端 未结 9 962
孤街浪徒
孤街浪徒 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: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");
                }
            }
        }
    }
    

提交回复
热议问题