C# selenium环境配置

时间:2023-03-08 21:25:02
1.下载C#selenium
  selenium官网:  http://www.seleniumhq.org/download/
 C# selenium环境配置
下载后解压:
 C# selenium环境配置
打开net35后,将里面的dll文件添加到ranorex中;
C# selenium环境配置
C# selenium环境配置
2.浏览器环境配置

将需要用到的浏览器chrome,firfox,等exe所在的文件

夹添加到系统变量path中去,必要的时候需要重启电脑;

这一步很重要,否则运行下面的脚本打开不了浏览器,需要在脚本中添加浏览器地址
3.本地运行脚本;
using Selenium;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
public void Test001()
        {
              IWebDriver selenium = new ChromeDriver();
              selenium.Navigate().GoToUrl("http://www.baidu.com/");
              //selenium.FindElement();
        }
4.在远程机执行remoting
远程机环境配置:
  • 需要将浏览器添加到系统变量path中
  • 需要下载selenium-server-standalond.jar
  • 然后再cmd或者windows PowerShell中进入到selenium-server-standalond.jar所在的文件夹,执行命令,打开服务,命令如下
  • PS C:\Users\cpr018\Desktop\selenium> java -jar selenium-server-standalone.jar
  • 在客户端运行脚本,即可在远程执行测试脚本
客户端脚本:
using Selenium;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
public static void test001()
        {
              //uri中的ip为远程机的ip,开启服务默认端口为4444
              var driver = new RemoteWebDriver(new Uri("http://192.168.226.131:4444/wd/hub"), DesiredCapabilities.Chrome());
              driver.Navigate().GoToUrl("http://www.baidu.com/");
 
 
         }