首页 技术 正文
技术 2022年11月12日
0 收藏 955 点赞 4,293 浏览 1315 个字

作者:Surpassme

来源:http://www.jianshu.com/p/33b87bc00233

声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢。

最近开始复习Selenium的相关知识,顺便整理自己的学习心得,分享出来也算是总结吧。

Step-1:准备所需的开发环境、浏览器驱动、Selenium-Webdriver、单元测试框架,因目前使用C#的开发神器都Visual Studio,本文也打算采用Visual Studio 2013

Visual Studio下载地址:https://www.visualstudio.com/

各浏览器驱动:www.seleniumhq.org/download

Selenium-Webdriver下载驱动:www.seleniumhq.org/download

单元测试框架:.NET里面的单元测试比较多,如NUint、MSTest、xUnit,本人打算采用MSTest,由Visual Studio自带,大家可根据自己喜好自行选择。

当然也可以从共享网盘下载,地址:https://pan.baidu.com/s/1kV6uzzx 密码:9m1q

Step-2:使用Visual Studio创建一个单元测试项目,如下图所示:

[Selenium With C#基础教程] Lesson-01环境搭建

Step-3:添加引用Webdriver,Selenium Webdriver目前支持的.NET平台有3.5和4.0,在这里我选择4.0

[Selenium With C#基础教程] Lesson-01环境搭建

Step-4:创建第一个单元测试用例,并编译通过

 using System;
//MS自带的单元测试框架
using Microsoft.VisualStudio.TestTools.UnitTesting;
//Webdriver引用
using OpenQA.Selenium;
using OpenQA.Selenium.IE; namespace SeleniumDemo
{
[TestClass]
public class Lesson01
{
[TestMethod]
public void Demo01()
{
IWebDriver driver = new InternetExplorerDriver();
//访问百度
driver.Navigate().GoToUrl("https://www.baidu.com");
//查找搜索输入框,输入Selenium
IWebElement searchText = driver.FindElement(By.Id("kw"));
//在输入前清空内容
searchText.Clear();
searchText.SendKeys("Selenium");
//查找元素
IWebElement searchBtn = driver.FindElement(By.Id("su"));
//点击搜索按钮
searchBtn.Click();
//退出浏览器
driver.Close();
}
}
}

Step-5:将Internet Explorer驱动程序IEDriverServer.exe复制到当前项目的Debug目录,并运行测试,如下图所示:

[Selenium With C#基础教程] Lesson-01环境搭建

Note:如果出现如下报错,则将IE浏览器选项中安全选项中的启用保护模式全部勾选上即可,如下图所示:

[Selenium With C#基础教程] Lesson-01环境搭建

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295