├── .gitattributes ├── .gitignore ├── .nuget ├── NuGet.Config ├── NuGet.exe └── NuGet.targets ├── BrowserDriver ├── IEDriverServer.exe └── chromedriver.exe ├── Demo.SeleniumTest.sln └── Demo.SeleniumTest ├── Demo.SeleniumTest.csproj ├── ExpectedConditionsExtension.cs ├── Properties └── AssemblyInfo.cs ├── TestCase ├── Lesson01_VisitCnblogs.cs ├── Lesson02_CoreObject.cs ├── Lesson03_FindElement.cs ├── Lesson04_05_SeleniumAPI.cs ├── Lesson06_TestFlowControl.cs └── Lesson07_WindowProcess.cs └── packages.config /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Nuget packages directory 2 | packages/ 3 | ## Ignore Visual Studio temporary files, build results, and 4 | ## files generated by popular Visual Studio add-ons. 5 | 6 | *.obj 7 | *.pdb 8 | *.exe 9 | *.lik 10 | *.suo 11 | *.dbmdl 12 | bin/ 13 | obj/ 14 | 15 | !/BrowserDriver/* 16 | SourceCode/.localhistory 17 | !/.nuget/* -------------------------------------------------------------------------------- /.nuget/NuGet.Config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.nuget/NuGet.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DemoCnblogs/Selenium/5606d403d278cdd2bddad42eb92134ceaf01b5a6/.nuget/NuGet.exe -------------------------------------------------------------------------------- /.nuget/NuGet.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(MSBuildProjectDirectory)\..\ 5 | 6 | 7 | false 8 | 9 | 10 | false 11 | 12 | 13 | true 14 | 15 | 16 | false 17 | 18 | 19 | 20 | 21 | 22 | 26 | 27 | 28 | 29 | 30 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget")) 31 | 32 | 33 | 34 | 35 | $(SolutionDir).nuget 36 | 37 | 38 | 39 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config 40 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config 41 | 42 | 43 | 44 | $(MSBuildProjectDirectory)\packages.config 45 | $(PackagesProjectConfig) 46 | 47 | 48 | 49 | 50 | $(NuGetToolsPath)\NuGet.exe 51 | @(PackageSource) 52 | 53 | "$(NuGetExePath)" 54 | mono --runtime=v4.0.30319 "$(NuGetExePath)" 55 | 56 | $(TargetDir.Trim('\\')) 57 | 58 | -RequireConsent 59 | -NonInteractive 60 | 61 | "$(SolutionDir) " 62 | "$(SolutionDir)" 63 | 64 | 65 | $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir) 66 | $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols 67 | 68 | 69 | 70 | RestorePackages; 71 | $(BuildDependsOn); 72 | 73 | 74 | 75 | 76 | $(BuildDependsOn); 77 | BuildPackage; 78 | 79 | 80 | 81 | 82 | 83 | 84 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 99 | 100 | 103 | 104 | 105 | 106 | 108 | 109 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /BrowserDriver/IEDriverServer.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DemoCnblogs/Selenium/5606d403d278cdd2bddad42eb92134ceaf01b5a6/BrowserDriver/IEDriverServer.exe -------------------------------------------------------------------------------- /BrowserDriver/chromedriver.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DemoCnblogs/Selenium/5606d403d278cdd2bddad42eb92134ceaf01b5a6/BrowserDriver/chromedriver.exe -------------------------------------------------------------------------------- /Demo.SeleniumTest.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo.SeleniumTest", "Demo.SeleniumTest\Demo.SeleniumTest.csproj", "{2E3DF5C2-8A38-4A03-86D7-8D463C917E47}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {2E3DF5C2-8A38-4A03-86D7-8D463C917E47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {2E3DF5C2-8A38-4A03-86D7-8D463C917E47}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {2E3DF5C2-8A38-4A03-86D7-8D463C917E47}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {2E3DF5C2-8A38-4A03-86D7-8D463C917E47}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/Demo.SeleniumTest.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Debug 7 | AnyCPU 8 | {2E3DF5C2-8A38-4A03-86D7-8D463C917E47} 9 | Library 10 | Properties 11 | Demo.SeleniumTest 12 | Demo.SeleniumTest 13 | v4.5 14 | 512 15 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 16 | 10.0 17 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 18 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 19 | False 20 | UnitTest 21 | ecb68935 22 | ..\ 23 | true 24 | 25 | 26 | true 27 | full 28 | false 29 | bin\Debug\ 30 | DEBUG;TRACE 31 | prompt 32 | 4 33 | 34 | 35 | pdbonly 36 | true 37 | bin\Release\ 38 | TRACE 39 | prompt 40 | 4 41 | 42 | 43 | 44 | 45 | 46 | ..\packages\Selenium.WebDriver.2.49.0\lib\net40\WebDriver.dll 47 | True 48 | 49 | 50 | ..\packages\Selenium.Support.2.49.0\lib\net40\WebDriver.Support.dll 51 | True 52 | 53 | 54 | ..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll 55 | True 56 | 57 | 58 | ..\packages\xunit.assert.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.assert.dll 59 | True 60 | 61 | 62 | ..\packages\xunit.extensibility.core.2.1.0\lib\portable-net45+win8+wp8+wpa81\xunit.core.dll 63 | True 64 | 65 | 66 | ..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll 67 | True 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | False 96 | 97 | 98 | False 99 | 100 | 101 | False 102 | 103 | 104 | False 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. 114 | 115 | 116 | 117 | 118 | 119 | 126 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/ExpectedConditionsExtension.cs: -------------------------------------------------------------------------------- 1 | using OpenQA.Selenium; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Demo.SeleniumTest 9 | { 10 | /// 11 | /// 自定义的扩展条件 12 | /// 13 | public class ExpectedConditionsExtension 14 | { 15 | /// 16 | /// 等待进度条消失 17 | /// 18 | /// WebDriver对象 19 | /// 操作Func对象 20 | public static Func ProcessBarDisappears() 21 | { 22 | return delegate(IWebDriver driver) 23 | { 24 | IWebElement element = null; 25 | try 26 | { 27 | element = driver.FindElement(By.XPath(".//div[@id='divProcessBar']")); 28 | } 29 | catch (NoSuchElementException) { return true; } 30 | return !element.Displayed; 31 | }; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Demo.SeleniumTest")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Demo.SeleniumTest")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("9abbd6a9-5f95-442a-88db-3fc1ebf374a7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/TestCase/Lesson01_VisitCnblogs.cs: -------------------------------------------------------------------------------- 1 | using OpenQA.Selenium; 2 | using OpenQA.Selenium.Firefox; 3 | using System; 4 | using System.Linq; 5 | using Xunit; 6 | 7 | namespace Demo.SeleniumTest 8 | { 9 | public class UnitTesCase 10 | { 11 | /// 12 | /// 访问博客园 13 | /// 14 | [Fact(DisplayName = "Cnblogs.Visit")] 15 | public void Visit_Cnblogs() 16 | { 17 | IWebDriver driver = new FirefoxDriver(); 18 | 19 | driver.Url = "http://www.cnblogs.com/NorthAlan"; 20 | var lnkAutomation = driver.FindElement(By.XPath(".//div[@id='sidebar_postcategory']/ul/li/a[text()='自动化测试']")); 21 | lnkAutomation.Click(); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Demo.SeleniumTest/TestCase/Lesson02_CoreObject.cs: -------------------------------------------------------------------------------- 1 | using OpenQA.Selenium; 2 | using OpenQA.Selenium.Firefox; 3 | using Xunit; 4 | using Xunit.Abstractions; 5 | 6 | namespace Demo.SeleniumTest 7 | { 8 | public class Lesson02_CoreObject 9 | { 10 | /// 11 | /// 输出对象 12 | /// 13 | private readonly ITestOutputHelper _output; 14 | /// 15 | /// 构造函数,初始化输出对象 16 | /// 17 | /// 注入输出对象 18 | public Lesson02_CoreObject(ITestOutputHelper output) 19 | { 20 | this._output = output; 21 | } 22 | 23 | /// 24 | /// demo1 : 获取元素 25 | /// 26 | [Fact(DisplayName = "Cnblogs.CoreObject.Demo1")] 27 | public void CoreObject_Demo1() 28 | { 29 | _output.WriteLine("Step 01 : 启动浏览器并打开博客园首页。"); 30 | IWebDriver driver = new FirefoxDriver(); 31 | driver.Url = "http://www.cnblogs.com"; 32 | driver.Manage().Window.Maximize(); 33 | 34 | _output.WriteLine("Step 02 : 获取并输出部分页面信息。"); 35 | _output.WriteLine(string.Format("Current window handle: {0}", driver.CurrentWindowHandle)); 36 | _output.WriteLine(string.Format("Window handle count: {0}", driver.WindowHandles.Count)); 37 | _output.WriteLine(string.Format("Current window title: {0}", driver.Title)); 38 | 39 | _output.WriteLine("Step 03 : 验证博客园站点的 Title 是否正确。"); 40 | Assert.Equal("博客园 - 开发者的网上家园", driver.Title); 41 | 42 | _output.WriteLine("Step 04 : 关闭当前页面。"); 43 | driver.Close(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/TestCase/Lesson03_FindElement.cs: -------------------------------------------------------------------------------- 1 | using OpenQA.Selenium; 2 | using OpenQA.Selenium.Firefox; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using Xunit; 7 | using Xunit.Abstractions; 8 | 9 | namespace Demo.SeleniumTest 10 | { 11 | public class Lesson03_FindElement 12 | { 13 | /// 14 | /// 输出对象 15 | /// 16 | private readonly ITestOutputHelper _output; 17 | /// 18 | /// 构造函数,初始化输出对象 19 | /// 20 | /// 注入输出对象 21 | public Lesson03_FindElement(ITestOutputHelper output) 22 | { 23 | this._output = output; 24 | } 25 | 26 | /// 27 | /// demo1 : 获取元素 28 | /// 29 | [Fact(DisplayName = "Cnblogs.CheckNavBar.Demo1")] 30 | public void CheckNavBar_GetElement() 31 | { 32 | _output.WriteLine("Step 01 : 启动浏览器并打开博客园首页。"); 33 | IWebDriver driver = new FirefoxDriver(); 34 | driver.Url = "http://www.cnblogs.com"; 35 | 36 | _output.WriteLine("Step 02 : 寻找需要检查的页面元素。"); 37 | var divMain = driver.FindElement(By.Id("main")); 38 | var lnkHome = driver.FindElement(By.XPath(".//ul[@class='post_nav_block']/li[1]/a")); 39 | var lnkEssence = driver.FindElement(By.XPath(".//ul[@class='post_nav_block']/li[2]/a")); 40 | var lnkCandidate = driver.FindElement(By.XPath(".//ul[@class='post_nav_block']/li[3]/a")); 41 | var lnkNews = driver.FindElement(By.XPath(".//ul[@class='post_nav_block']/li[4]/a")); 42 | 43 | _output.WriteLine("Step 03 : 检查导航条文字信息。"); 44 | Assert.Equal("首页", lnkHome.Text); 45 | Assert.Equal("精华", lnkEssence.Text); 46 | Assert.Equal("候选", lnkCandidate.Text); 47 | Assert.Equal("新闻", lnkNews.Text); 48 | 49 | _output.WriteLine("Step 04 : 关闭浏览器。"); 50 | driver.Close(); 51 | } 52 | 53 | 54 | [Fact(DisplayName = "Cnblogs.CheckNavBar.Demo2")] 55 | public void CheckNavBar_GetElements() 56 | { 57 | _output.WriteLine("Step 00 : 准备测试数据。"); 58 | var testDatas = new List() { "首页", "精华", "候选", "新闻" }; //准备测试数据 59 | 60 | _output.WriteLine("Step 01 : 启动浏览器并打开博客园首页。"); 61 | IWebDriver driver = new FirefoxDriver(); 62 | driver.Url = "http://www.cnblogs.com"; 63 | 64 | _output.WriteLine("Step 02 : 寻找需要检查的页面元素。"); 65 | var divMain = driver.FindElement(By.Id("main")); 66 | var lnkNavList = driver.FindElements(By.XPath(".//ul[@class='post_nav_block']/li[1]/a")); 67 | 68 | _output.WriteLine("Step 03 : 检查导航条文字信息。"); 69 | for (var i = 0; i < lnkNavList.Count; i++) 70 | { 71 | Assert.Equal(testDatas[i], lnkNavList[i].Text); 72 | } 73 | 74 | _output.WriteLine("Step 04 : 关闭浏览器。"); 75 | driver.Close(); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/TestCase/Lesson04_05_SeleniumAPI.cs: -------------------------------------------------------------------------------- 1 |  2 | using OpenQA.Selenium; 3 | using OpenQA.Selenium.Firefox; 4 | using OpenQA.Selenium.Interactions; 5 | using OpenQA.Selenium.Support.UI; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Drawing; 9 | using System.Drawing.Imaging; 10 | using System.Linq; 11 | using Xunit; 12 | using Xunit.Abstractions; 13 | 14 | namespace Demo.SeleniumTest 15 | { 16 | public class Lesson04_05_SeleniumAPI 17 | { 18 | /// 19 | /// 输出对象 20 | /// 21 | private readonly ITestOutputHelper _output; 22 | /// 23 | /// 构造函数,初始化输出对象 24 | /// 25 | /// 注入输出对象 26 | public Lesson04_05_SeleniumAPI(ITestOutputHelper output) 27 | { 28 | this._output = output; 29 | } 30 | 31 | /// 32 | /// demo1 : 检查首页文本 33 | /// 34 | [Fact(DisplayName = "Cnblogs.SeleniumAPI.Demo1")] 35 | public void SeleniumAPI_Demo1() 36 | { 37 | _output.WriteLine("Step 01 : 启动浏览器并打开博客园首页。"); 38 | IWebDriver driver = new FirefoxDriver(); 39 | driver.Url = "http://www.cnblogs.com"; 40 | 41 | _output.WriteLine("Step 02 : 寻找需要检查的页面元素。"); 42 | var lnkHome = driver.FindElement(By.XPath(".//ul[@class='post_nav_block']/li[1]/a")); 43 | var value = lnkHome.GetAttribute("class"); 44 | lnkHome.GetCssValue("font-family"); 45 | lnkHome.GetCssValue("color"); 46 | 47 | _output.WriteLine(string.Format("导航内栏内容:{0}", lnkHome.Text)); 48 | 49 | _output.WriteLine("Step 04 : 关闭浏览器。"); 50 | driver.Close(); 51 | } 52 | 53 | 54 | /// 55 | /// demo2 : 检查首页文本的属性,字体和颜色 56 | /// 57 | [Fact(DisplayName = "Cnblogs.SeleniumAPI.Demo2")] 58 | public void SeleniumAPI_Demo2() 59 | { 60 | _output.WriteLine("Step 01 : 启动浏览器并打开博客园首页。"); 61 | IWebDriver driver = new FirefoxDriver(); 62 | driver.Url = "http://www.cnblogs.com"; 63 | 64 | _output.WriteLine("Step 02 : 寻找需要检查的页面元素。"); 65 | var lnkHome = driver.FindElement(By.XPath(".//ul[@class='post_nav_block']/li[1]/a")); 66 | 67 | lnkHome.GetCssValue("color"); 68 | 69 | _output.WriteLine(string.Format("导航内栏内容:{0}", lnkHome.Text)); 70 | _output.WriteLine(string.Format("classs属性内容:{0}", lnkHome.GetAttribute("class"))); 71 | _output.WriteLine(string.Format("字体:{0}", lnkHome.GetCssValue("font-family"))); 72 | _output.WriteLine(string.Format("颜色:{0}", lnkHome.GetCssValue("color"))); 73 | 74 | _output.WriteLine("Step 04 : 关闭浏览器。"); 75 | driver.Close(); 76 | } 77 | 78 | /// 79 | /// demo3 : 首页查询 80 | /// 81 | [Fact(DisplayName = "Cnblogs.SeleniumAPI.Demo3")] 82 | public void SeleniumAPI_Demo3() 83 | { 84 | _output.WriteLine("Step 01 : 启动浏览器并打开博客园首页。"); 85 | IWebDriver driver = new FirefoxDriver(); 86 | driver.Url = "http://www.cnblogs.com"; 87 | 88 | _output.WriteLine("Step 02 : 寻找需要操作的页面元素。"); 89 | var txtSearch = driver.FindElement(By.Id("zzk_q")); 90 | var btnSearch = driver.FindElement(By.XPath(".//input[@type='button' and @value='找找看']")); 91 | 92 | _output.WriteLine("Step 03 : 输入查询文本"); 93 | txtSearch.SendKeys("小北De编程手记"); 94 | 95 | _output.WriteLine("Step 04 : 点击查询"); 96 | btnSearch.Click(); 97 | 98 | System.Threading.Thread.Sleep(5000); 99 | 100 | _output.WriteLine("Step 05 : 关闭浏览器。"); 101 | driver.Close(); 102 | } 103 | 104 | /// 105 | /// demo4 : 复杂交互 106 | /// 107 | [Fact(DisplayName = "Cnblogs.SeleniumAPI.Demo4")] 108 | public void SeleniumAPI_Demo4() 109 | { 110 | _output.WriteLine("Step 01 : 启动浏览器并打开博客园首页。"); 111 | IWebDriver driver = new FirefoxDriver(); 112 | driver.Url = "http://www.cnblogs.com"; 113 | 114 | _output.WriteLine("Step 02 : 寻找需要操作的页面元素。"); 115 | var divText = driver.FindElement(By.Id("site_nav_top")); 116 | var point = divText.Location; 117 | var width = int.Parse(divText.GetCssValue("width").Replace("px", string.Empty)); 118 | 119 | _output.WriteLine("Step 03 : 选中文本信息。"); 120 | var action = new Actions(driver); 121 | action 122 | .MoveByOffset(point.X, point.Y) //移动鼠标到文本开头 123 | .ClickAndHold() //按下鼠标 124 | .MoveByOffset(point.X + width, point.Y) //移动鼠标到文本结束 125 | .Release() //释放鼠标 126 | .Build() 127 | .Perform(); 128 | 129 | System.Threading.Thread.Sleep(5000); 130 | _output.WriteLine("Step 04 : 关闭浏览器。"); 131 | driver.Close(); 132 | } 133 | 134 | /// 135 | /// demo5 : SelectElement 136 | /// 137 | [Fact(DisplayName = "Cnblogs.SeleniumAPI.Demo5", Skip = "Just Demo")] 138 | public void SeleniumAPI_Demo5() 139 | { 140 | _output.WriteLine("Step 01 : 启动浏览器并打开某个网站。"); 141 | IWebDriver driver = new FirefoxDriver(); 142 | driver.Url = "http://www.xxx.com"; 143 | 144 | _output.WriteLine("Step 02 : 寻找需要操作的页面元素。"); 145 | var dllDom = (SelectElement)driver.FindElement(By.Id("selectDomId")); 146 | var isMultiple = dllDom.IsMultiple; 147 | var option = dllDom.SelectedOption; 148 | dllDom.SelectByIndex(1); 149 | dllDom.SelectByText("Text"); 150 | dllDom.SelectByValue("Value"); 151 | 152 | //单选按钮 153 | var radioA = driver.FindElement(By.Id("radioA_Id")); 154 | var radioB = driver.FindElement(By.Id("radioB_Id")); 155 | radioB.Click(); 156 | var isSelected = radioA.Selected; 157 | 158 | _output.WriteLine("Step 03 : 关闭浏览器。"); 159 | driver.Close(); 160 | } 161 | 162 | /// 163 | /// demo6 : 截屏 164 | /// 165 | [Fact(DisplayName = "Cnblogs.SeleniumAPI.Demo6")] 166 | public void SeleniumAPI_Demo6() 167 | { 168 | _output.WriteLine("Step 01 : 启动浏览器并打开博客园首页。"); 169 | IWebDriver driver = new FirefoxDriver(); 170 | driver.Url = "http://www.cnblogs.com"; 171 | 172 | _output.WriteLine("Step 02 : 寻找需要操作的页面元素。"); 173 | var txtSearch = driver.FindElement(By.Id("zzk_q")); 174 | var btnSearch = driver.FindElement(By.XPath(".//input[@type='button' and @value='找找看']")); 175 | 176 | _output.WriteLine("Step 03 : 输入查询文本&点击查询"); 177 | txtSearch.SendKeys("小北De编程手记"); 178 | btnSearch.Click(); 179 | 180 | _output.WriteLine("Step 04 : 截屏"); 181 | var takesScreenshot = (ITakesScreenshot)driver; 182 | var screenshot = takesScreenshot.GetScreenshot(); 183 | screenshot.SaveAsFile("screenshot.png", ImageFormat.Png); 184 | 185 | _output.WriteLine("Step 05 : 关闭浏览器。"); 186 | driver.Close(); 187 | } 188 | } 189 | } 190 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/TestCase/Lesson06_TestFlowControl.cs: -------------------------------------------------------------------------------- 1 | using OpenQA.Selenium; 2 | using OpenQA.Selenium.Firefox; 3 | using OpenQA.Selenium.Support.UI; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using Xunit; 10 | using Xunit.Abstractions; 11 | 12 | namespace Demo.SeleniumTest 13 | { 14 | public class Lesson06_TestFlowControl 15 | { 16 | /// 17 | /// 输出对象 18 | /// 19 | private readonly ITestOutputHelper _output; 20 | /// 21 | /// 构造函数,初始化输出对象 22 | /// 23 | /// 注入输出对象 24 | public Lesson06_TestFlowControl(ITestOutputHelper output) 25 | { 26 | this._output = output; 27 | } 28 | 29 | /// 30 | /// demo1 : 设置隐式等待同步策略 31 | /// 32 | [Fact(DisplayName = "Cnblogs.TestFlowControl.Demo1")] 33 | public void TestFlowControl_Demo1() 34 | { 35 | IWebDriver driver = new FirefoxDriver(); 36 | // 1. 隐式的等待 同步测试 37 | driver.Manage().Timeouts() 38 | .ImplicitlyWait(TimeSpan.FromSeconds(10)) 39 | .SetPageLoadTimeout(TimeSpan.FromSeconds(10)) 40 | .SetScriptTimeout(TimeSpan.FromSeconds(10)); 41 | 42 | driver.Close(); 43 | } 44 | 45 | 46 | /// 47 | /// demo2 :设置显示等待同步策略 48 | /// 49 | [Fact(DisplayName = "Cnblogs.TestFlowControl.Demo2")] 50 | public void TestFlowControl_Demo2() 51 | { 52 | IWebDriver driver = new FirefoxDriver(); 53 | //省略操作代码.... 54 | WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 55 | wait.Until(ExpectedConditions.ElementExists(By.Id("Object ID"))); 56 | } 57 | 58 | /// 59 | /// demo3 : 扩展等待 60 | /// 61 | [Fact(DisplayName = "Cnblogs.TestFlowControl.Demo3")] 62 | public void TestFlowControl_Demo3() 63 | { 64 | IWebDriver driver = new FirefoxDriver(); 65 | //省略操作代码.... 66 | WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 67 | wait.Until( 68 | delegate(IWebDriver dir) 69 | { 70 | var element = dir.FindElement(By.XPath(".//div[@id='divProcessBar']")); 71 | return !element.Displayed; 72 | } 73 | ); 74 | } 75 | 76 | /// 77 | /// demo4 : 扩展等待 升级版 78 | /// 79 | [Fact(DisplayName = "Cnblogs.TestFlowControl.Demo4")] 80 | public void TestFlowControl_Demo4() 81 | { 82 | IWebDriver driver = new FirefoxDriver(); 83 | //省略操作代码.... 84 | WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 85 | wait.Until(ExpectedConditionsExtension.ProcessBarDisappears()); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/TestCase/Lesson07_WindowProcess.cs: -------------------------------------------------------------------------------- 1 | using OpenQA.Selenium; 2 | using OpenQA.Selenium.Firefox; 3 | using OpenQA.Selenium.Support.UI; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using Xunit; 10 | using Xunit.Abstractions; 11 | 12 | namespace Demo.SeleniumTest.TestCase 13 | { 14 | public class Lesson07_WindowProcess 15 | { 16 | /// 17 | /// 输出对象 18 | /// 19 | private readonly ITestOutputHelper _output; 20 | /// 21 | /// 构造函数,初始化输出对象 22 | /// 23 | /// 注入输出对象 24 | public Lesson07_WindowProcess(ITestOutputHelper output) 25 | { 26 | this._output = output; 27 | } 28 | 29 | /// 30 | /// demo1 : 获取目标定位对象 31 | /// 32 | [Fact(DisplayName = "Cnblogs.WindowProcess.Demo1", Skip = "Just Demo")] 33 | public void WindowProcess_Demo1() 34 | { 35 | // 1. 获取窗口定位对象 36 | IWebDriver driver = new FirefoxDriver(); 37 | //省略部分代码... ... 38 | ITargetLocator locator = driver.SwitchTo(); 39 | driver = locator.Window("windowName"); 40 | //后续操作... ... 41 | driver.Quit(); 42 | } 43 | 44 | /// 45 | /// demo2 : 根据标题定位元素 46 | /// 47 | [Fact(DisplayName = "Cnblogs.WindowProcess.Demo2")] 48 | public void WindowProcess_Demo2() 49 | { 50 | var articleName = "[小北De编程手记] : Lesson 02 - Selenium For C# 之 核心对象"; 51 | 52 | _output.WriteLine("Step 01 : 启动浏览器并打开Lesson 01 - Selenium For C#"); 53 | IWebDriver driver = new FirefoxDriver(); 54 | driver.Url = "http://www.cnblogs.com/NorthAlan/p/5155915.html"; 55 | 56 | _output.WriteLine("Step 02 : 点击链接打开新页面。"); 57 | var lnkArticle02 = driver.FindElement(By.LinkText(articleName)); 58 | lnkArticle02.Click(); 59 | 60 | _output.WriteLine("Step 03 : 根据标题获取新页面的句柄。"); 61 | var oldWinHandle = driver.CurrentWindowHandle; 62 | foreach (var winHandle in driver.WindowHandles) 63 | { 64 | driver.SwitchTo().Window(winHandle); 65 | if (driver.Title.Contains(articleName)) 66 | { 67 | break; 68 | } 69 | } 70 | 71 | _output.WriteLine("Step 04 : 验证新页面标题是否正确。"); 72 | var articleTitle = driver.FindElement(By.Id("cb_post_title_url")); 73 | Assert.Equal(articleName, articleTitle.Text); 74 | 75 | _output.WriteLine("Step 05 : 关闭浏览器。"); 76 | driver.Quit(); 77 | 78 | } 79 | 80 | /// 81 | /// demo3 : 处理Alert 82 | /// 83 | [Fact(DisplayName = "Cnblogs.WindowProcess.Demo3", Skip = "Just Demo")] 84 | public void WindowProcess_Demo3() 85 | { 86 | 87 | IWebDriver driver = new FirefoxDriver(); 88 | //省略部分代码... ... 89 | var oldWinHandle = driver.CurrentWindowHandle; 90 | ITargetLocator locator = driver.SwitchTo(); 91 | IAlert winAlert = locator.Alert(); 92 | winAlert.Accept(); //确定:Alert , Confirm, Prompt 93 | winAlert.Dismiss(); //取消:Confirm, Prompt 94 | var text = winAlert.Text; //获取提示内容:Alert , Confirm, Prompt 95 | winAlert.SendKeys("input text."); //输入提示文本:Prompt 96 | 97 | //后续操作... ... 98 | driver.Quit(); 99 | 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /Demo.SeleniumTest/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | --------------------------------------------------------------------------------