Selenium 简明教程
Selenium with C
Selenium 可以与多种语言一起使用,如 Java、Python、JavaScript、Ruby、C# 等。Selenium 被广泛用于 Web 自动化测试。Selenium 是一种开放源代码、可移植的自动软件测试工具,用于测试 Web 应用程序。
Selenium can be used with multiple languages like Java, Python, JavaScript, Ruby, C#, and so on. Selenium is used extensively for web automation testing. Selenium is an open-source and a portable automated software testing tool for testing web applications.
它具有跨不同浏览器和操作系统工作的功能。Selenium 不仅是一个单一的工具,而且是一组工具,帮助测试人员更有效地自动化基于 Web 的应用程序。C# 是 Selenium 的另一种语言绑定。
It has capabilities to operate across different browsers and operating systems. Selenium is not just a single tool but a set of tools that helps testers to automate web-based applications more efficiently. C# is another language binding for Selenium.
Selenium 中的 Java 和 C# 语言绑定没有太大差异。主要区别仅在于语言。此外,这两种语言的变更日志也有所不同。
There are no major differences between the Java and C# language bindings in Selenium. The main difference lies in the language only. Also, there is a difference in the Change log of both of these languages.
在 Java 中,我们说使用 Webdriver,WebElement,但在 C# 中,我们称之为 IWebdriver,IWebElement。因此,在 C# 中遵循的约定是,在声明接口时,在接口名称之前添加字母“I”。
In Java, we say use the Webdriver, WebElement but in C# we call it IWebdriver, IWebElement. So the convention which is followed in C# is that, while declaring an interface, the letter ‘I’ is added prior to the name of the Interface.
在 Java 中使用 Page Factory 时,使用 @FindBy 注释标识元素,但是,在 C# 中使用 Page Factory 时,它称为属性并表示为 [FindBy] 用于查找元素。
In Page Factory using Java, the element is identified using the @FindBy annotations, however, while using the Page Factory in C#, it is known as an attribute and denoted by [FindBy] is used to locate elements.
Setup Selenium with C
Step 1 − 从链接中安装名为 Visual Studio 的 C# 代码编辑器 − https://visualstudio.microsoft.com/downloads/ 。
Step 1 − Install the C# code editor called Visual Studio from the link − https://visualstudio.microsoft.com/downloads/.
使用此编辑器,我们可以开始处理 C# 项目以启动我们的测试自动化。
Using this editor, we can start working on a C# project to start our test automation.
要更详细地了解如何设置 Visual Studio,请参阅链接 − https://www.tutorialspoint.com/ebook/ 。
To get a more detailed view on how to set up the Visual Studio, refer to the link − https://www.tutorialspoint.com/ebook/.
Step 2 − 创建一个新项目,方法是右键单击现有项目,然后单击“添加”选项,然后单击“新建项目”,或者如果没有打开任何项目,则单击“文件”菜单,然后选择“新建项目”选项。
Step 2 − Create a new project, by either right clicking on an existing project, and click on the Add option then click on the New Project, or if no project is opened, click on the File menu, then select the New Project option.
Step 3 − 选择“控制台应用程序”选项,然后单击“下一步”按钮。
Step 3 − Select the option Console Application, then click on the Next button.
Step 4 − 输入项目名称,比如 SeleniumTest,然后点击创建按钮。
Step 4 − Enter a project name, say SeleniumTest, then click on the Create button.
Step 5 − 新建立的项目 - SeleniumTest 应该会可见。
Step 5 − The newly created project - SeleniumTest should be visible.
Step 6 − 右键点击新建立的项目,选择选项“管理 NuGet 包”。
Step 6 − Right click on the newly created project and select the option Manage NuGet Packages.
Step 7 − 在右上角的搜索框中输入 selenium,便会显示所有基于 selenium 搜索的包。点击 Selenium.WebDriver 并点击“添加包”按钮。安装所有与 Selenium 相关的包。
Step 7 − Enter selenium in the search box at the top right, all the packages based on selenium search should display. Click on the Selenium.WebDriver and click on the Add Packages button. Install all the Selenium related packages.
Step 8 − 完成后,Selenium.Webdriver 成功添加一条消息,消息会显示在 Visual Studio 的顶部。
Step 8 − Once done, Selenium.Webdriver successfully added a message to display at the top of the Visual Studio.
Step 9 − 重启 Visual Studio。重新启动后,该解决方案资源管理器中的 NuGet 文件夹中应该会显示所有已安装的包。
Step 9 − Restart the Visual Studio. After it is launched again, all the installed packages should be displayed under the NuGet folder in the Solution Explorer.
Step 10 − 编写一段代码以获得页面标题 - Selenium Practice - Modal Dialogs 的下述页面。
Step 10 − Write a code to obtain the page title - Selenium Practice - Modal Dialogs of the below page.
Example
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumTest {
class Program {
static void Main(string[] args) {
// Initiate Webdriver
IWebDriver driver = new ChromeDriver();
// adding an implicit wait of 20 secs
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
// launch the application
driver.Navigate().GoToUrl("https://www.tutorialspoint.com/selenium/practice/modal-dialogs.php");
// get the page title
String pageTitle = driver.Title;
Console.WriteLine("Page title is: " + pageTitle);
}
}
}
Page title is: Selenium Practice - Modal Dialogs
在上例中,我们启动了一个应用程序并用消息 - Page title is: Selenium Practice - Modal Dialogs 在控制台中获取了其页面标题。
In the above example, we had launched an application and obtained its page title in the console with the message - Page title is: Selenium Practice - Modal Dialogs.
Launch Browser and Quit Driver with Selenium C
我们可以使用 driver.Navigate().GoToUrl 方法启动浏览器并打开应用程序,最后使用 Quit() 方法退出浏览器。
We can launch the browser and open an application using the driver.Navigate().GoToUrl method, and finally quit the browser with the Quit() method.
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumTest {
class Program {
static void Main(string[] args){
// Initiate Webdriver
IWebDriver driver = new ChromeDriver();
// adding an implicit wait of 20 secs
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
// launch the application
driver.Navigate().GoToUrl("https://www.tutorialspoint.com/selenium/practice/check-box.php");
// quitting browser
driver.Quit();
}
}
}
在上例中,我们首先在 Chrome 浏览器中启动了一个应用程序,然后退出浏览器。
In the above example, we had first launched an application in the Chrome browser then quit the browser.
Identify Element and Check Its Functionality Using Selenium C
一旦我们导航到某个网页,我们必须与该页面中可用的 web 元素进行交互,比如点击链接/按钮、在编辑框中输入文本等,以完成我们的自动化测试用例。
Once we navigate to a webpage, we have to interact with the web elements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.
为此,我们的首要工作是标识该元素。为此,Selenium 中有一些可用的定位符,它们分别是 id、class、classname、name、linktext、partiallinktext、tagname、css 和 xpath。这些定位符需要与 FindElement() 方法一起使用。
For this, our first job is to identify the element. There are some locators available in Selenium for this purpose, they are id, class, class name, name, link text, partial link text, tagname, css, and xpath. These locators need to be used with the FindElement() method.
比如,driver.FindElement(By.Id("id")) 将定位给定 id 属性值的第一个 web 元素。如果没有任何元素具有匹配的 id 属性值,则应该抛出 NoSuchElementException。
For example, driver.FindElement(By.Id("id")) will locate the first web element with the given id attribute value. In case there is no element with the matching value of the id attribute, NoSuchElementException should be thrown.
我们来看看在下方图像中高亮显示的 Impressive 标签旁边的单选按钮的 html 代码 −
Let us see the html code of the radio button beside the Impressive label highlighted in the below image −
<input class="form-check-input" type="radio" name="tab"
value="igotthree" onclick="show3();">
让我们点击该单选按钮,然后便会在网页上看到文本 You have checked Impressive 。
Let us click on that radio button after which we would get the text You have checked Impressive on the web page.
Example
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumTest {
class Program {
static void Main(string[] args){
// Initiate the Webdriver
IWebDriver driver = new ChromeDriver();
// adding an implicit wait of 20 secs
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
// launch an application
driver.Navigate().GoToUrl("https://www.tutorialspoint.com/selenium/practice/radio-button.php");
// identify a radio button then click on radio button
IWebElement r = driver.FindElement
(By.XPath("/html/body/main/div/div/div[2]/form/div[3]/input"));
r.Click();
// identify text after clicking radio button
IWebElement txt = driver.FindElement(By.Id("check1"));
// obtain text
String text = txt.Text;
Console.WriteLine("Text is: " + text);
// quitting browser
driver.Quit();
}
}
}
Text is: You have checked Impressive
在上例中,我们在点击 Impressive 标签旁边的单选按钮后获得了文本,消息显示在控制台中 - You have checked Impressive 。
In the above example, we had obtained the text after clicking the radio button beside the label Impressive with the message in the console - You have checked Impressive.
这总结了我们对 Selenium 教程 - C# 教程的全面讲解。我们从描述如何使用 C# 设置 Selenium 并启动浏览器开始,以及如何使用 Selenium C# 标识元素并检查其功能。
This concludes our comprehensive take on the tutorial on Selenium - C# Tutorial. We’ve started with describing how to set up Selenium with C# and launch a browser, and how to identify an element and check its functionality using Selenium C#.
这使你掌握了 Selenium - C# 教程的深度知识。明智的做法是不断练习你学到的知识并探索与 Selenium 相关的其它知识,以加深你的理解并拓展你的视野。
This equips you with in-depth knowledge of the Selenium - C# Tutorial. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.