Selenium 4 is launching “soon”. The most exciting news: Selenium will be W3C standardized.

Selenium4即将“发射”。 最令人振奋的消息是:Selenium将被W3C标准化。

Browsers, such as Chrome, Firefox, Safari are all following W3C standardization, so from now on browser drivers will interact with Selenium WebDriver in W3C standard protocol.

Chrome,Firefox,Safari等浏览器都遵循W3C标准化,因此从现在开始,浏览器驱动程序将以W3C标准协议与Selenium WebDriver交互。

Why is this very, very cool? To date, some Selenium commands worked differently on different browsers. As automation engineers, we had to modify our code to deal with those changes. The new version will bring standardization and stability and will not require you to modify your code to work with different browsers. Cheers to that!

为什么这非常非常酷? 迄今为止,某些Selenium命令在不同的浏览器上的工作方式有所不同。 作为自动化工程师,我们必须修改代码以应对这些更改。 新版本将带来标准化和稳定性,并且不需要您修改代码即可与其他浏览器一起使用。 为此加油!

Prepare the local environment

准备当地环境

I will be working with C# Selenium WebDriver so let’s download and install the latest .NET Core.

我将使用C#Selenium WebDriver,因此让我们下载并安装最新的.NET Core。

Download the .NET Core DSK kit from the Microsoft site
从Microsoft网站下载.NET Core DSK工具包
Install the dotnet SDK
安装dotnet SDK

After installation, open the CMD prompt and type in “dotnet”. You should see the message like shown below:

安装后,打开CMD提示符并键入“ dotnet”。 您应该看到如下所示的消息:

Awesome, .NET Core is now up on a local machine and we should start using it, right? Well, in order to proceed, we will need an IDE. I like to use Visual Studio so let’s install the latest Community version.

太棒了,.NET Core现在位于本地计算机上,我们应该开始使用它,对吗? 好吧,为了继续进行,我们将需要一个IDE。 我喜欢使用Visual Studio,所以让我们安装 最新的社区版本。

Download and install the VS Community version
下载并安装VS社区版本

Now Select .NET desktop development and click Install.

现在,选择.NET桌面开发,然后单击安装

After installation, run the Visual Studio and select “Create a new project”:

安装后,运行Visual Studio并选择“创建新项目”:

Pick NUnit Test Project (.NET Core) and click the Next button.

选择NUnit测试项目(.NET Core),然后单击“下一步”按钮。

Select NUnit Test Project
选择NUnit测试项目

Name it and click on Create.

为其命名并单击Create

I named it “HelloWorld”
我将其命名为“ HelloWorld”

Your screen should look like this:

您的屏幕应如下所示:

Default unit test
默认单元测试

Select View > Test Explorer to bring up the Test Explorer window.

选择查看>测试资源管理器以打开“测试资源管理器”窗口。

Click on the Run and verify result.

单击运行并验证结果。

Initial test passed
初步测试通过

In order to install necessary packages, navigate to Project > Manage NuGet Packages, search for “selenium” and Install first three from the list:

为了安装必要的软件包,请导航至“项目”>“管理NuGet软件包”,搜索“ selenium”并从列表中安装前三个:

  • Selenium.WebDriverSelenium.WebDriver
  • Selenium.SupportSelenium支持
  • Selenium.WebDriver.ChromeDriverSelenium.WebDriver.ChromeDriver

Update the UnitTest1.cs class like shown below and run the test. The test will open the Chrome browser. You should navigate to the Google home page, validate the page title, and close the session.

如下所示更新UnitTest1.cs类,然后运行测试。 测试将打开Chrome浏览器。 您应该导航到Google主页,验证页面标题,然后关闭会话。

using NUnit.Framework;using OpenQA.Selenium;using OpenQA.Selenium.Chrome;namespace HelloWorld{    public class Tests    {        public IWebDriver driver;[SetUp]        public void Setup()        {            driver = new ChromeDriver();            driver.Navigate().GoToUrl("https://www.google.com/");        }[Test]        public void Test1()        {            Assert.AreEqual("Google", driver.Title);        }[TearDown]        public void Close()        {            driver.Close();        }    }}

Move to Selenium 4 alpha

移至Selenium 4 alpha

Now for the exciting part: let’s move to the latest Selenium 4.0.0 alpha 5 version!

现在开始激动人心的部分:让我们转到最新的Selenium 4.0.0 alpha 5版本

Bring up the Package Manager Console (View > Other Windows > Package Manager Console)…

调出Package Manager控制台(视图>其他Windows> Package Manager控制台)…

…and type in the commands shown below, one by one:

…并一一输入以下命令:

Install-Package Selenium.Support -Version 4.0.0-alpha05

安装软件包Selenium.Support-版本4.0.0-alpha05

Install-Package Selenium.WebDriver -Version 4.0.0-alpha05

安装软件包Selenium.WebDriver-版本4.0.0-alpha05

Find packages updated to the new version:

查找更新为新版本的软件包:

Go back to UnitTest1.cs class select ChromeDriver() and click f12:

返回UnitTest1.cs类,选择ChromeDriver()并单击f12:

We are now using Selenium 4 alpha. Good!

我们现在正在使用Selenium 4 alpha。 好!

Selenium 4 Relative Locators

Selenium4相对定位器

Relative aka Friendly locators are the brand new way of fetching an UI web element by using the location of another (known) element. There are five overloaded methods that can accept By or IWebElement parameters:

相对(又称友好)定位器是使用另一个(已知)元素的位置来获取UI Web元素的全新方法。 有五个可以接受By或IWebElement参数的重载方法:

  1. Above — finds an element that’s above a known element

    上方-查找已知元素上方的元素

  2. Below — finds an element that’s below a known element

    低于-查找低于已知元素的元素

  3. LeftOf — finds an element that sits on the left of a known element

    LeftOf —查找位于已知元素左侧的元素

  4. RightOf — finds an element that sits on the right of a known element

    RightOf-查找位于已知元素右边的元素

  5. Near — finds an element that sits in a circle of 50 pixels of a known element. Distance is configurable

    “附近” —查找一个位于已知元素的50像素圆内的元素。 距离是可配置的

All of those methods are called on a static RelativeBy.WithTagName(<tag of the searched element>) method. If you think about it, it definitely makes sense to use some sort of “element identification”. For example, if we use LeftOf() method, we can found loads of elements that sit on the left side such as input fields, lists, buttons, images, etc. With WithTagName() method, we say what kind of element we are searching for, by its tag name.

所有这些方法都在静态RelativeBy.WithTagName(<搜索元素的标签>)方法上调用。 如果您考虑一下,使用某种“元素识别”绝对是有意义的。 例如,如果我们使用LeftOf()方法,我们可以找到位于左侧的大量元素,例如输入字段,列表,按钮,图像等。使用WithTagName()方法,我们可以说我们是哪种元素通过其标签名称进行搜索。

Search for the element on the right
搜索右边的元素

See the sample above; in this case, we are searching for a button that sits on the right side of a header logo image.

参见上面的示例; 在这种情况下,我们正在搜索位于标题徽标图像右侧的按钮。

What is even better, we can combine those methods and find an element more efficient way. I like to call it Super Mario way. The same way we use controls to climb stairs in the arcade game.

更好的是,我们可以将这些方法结合起来,找到更有效的元素。 我喜欢称之为超级马里奥方式。 我们在街机游戏中使用控件爬楼梯的方式相同。

Run, Super Mario, run!

快跑,超级马里奥,快跑!

I updated the HelloWorld project used in the begging. Let’s get to work!

我更新了乞讨中使用的HelloWorld项目。 让我们开始工作吧!

using NUnit.Framework;using OpenQA.Selenium;using OpenQA.Selenium.Chrome;namespace HelloWorld{    public class Tests    {        public IWebDriver driver;[SetUp]        public void Setup()        {            driver = new ChromeDriver();            //using a demo online shopping app            driver.Navigate().GoToUrl("http://automationpractice.com/index.php");            driver.Manage().Window.Maximize();        }[Test]        public void Test1()        {            //this is an old way of using WebDriver - just to make sure it works on version 4.0.0 :)            Assert.AreEqual("My Store", driver.Title);            IWebElement SearchInput = driver.FindElement(By.Name("search_query"));            IWebElement SearchButton = driver.FindElement(By.Name("submit_search"));            SearchInput.SendKeys("demo");            SearchButton.Click();//try to click on the search button that's above menu            SearchInput = driver.FindElement(By.Name("search_query"));            SearchInput.SendKeys(" demo 2");            driver.FindElement(RelativeBy.WithTagName("button").Above(By.Id("block_top_menu"))).Click();            SearchInput = driver.FindElement(By.Name("search_query"));            SearchInput.SendKeys(" xxx");//try to click on the search button looking from the logo (it's on the right side)            driver.FindElement(RelativeBy.WithTagName("button").RightOf(By.Id("header_logo"))).Click();//click on the Logo image that's on the left of search input field and below the telephone icon            driver.FindElement(RelativeBy.WithTagName("a")                .LeftOf(By.Id("search_query_top"))                .Below(By.ClassName("icon-phone"))).Click();//add black blose to the chart. the second on the list            driver.FindElement(RelativeBy.WithTagName("span")                .RightOf(By.XPath("(//ul[@id='homefeatured']//img)[1]"))                ).Click();        }[TearDown]        public void Close()        {            driver.Close();        }    }}

Above() method

Above()方法

See the code given below; we are trying to click on an element with the tag “button” that sits above the menu.

参见下面给出的代码; 我们正在尝试单击菜单上方带有“按钮”标签的元素。

driver.FindElement(RelativeBy.WithTagName("button").Above(By.Id("block_top_menu"))).Click();

Click on the element on the left, below a known one

单击一个已知元素下方的左侧元素

In this case, we are trying to click on the company logo that will take us to a home page. It is located right under the telephone icon and has an input field, and has an element with id = ‘search_query_top’ on the right.

在这种情况下,我们尝试单击公司徽标,该徽标将带我们进入主页。 它位于电话图标的正下方,具有输入字段,并且在右侧具有id ='search_query_top'的元素。

Note: besides using By.Id and By.ClassName we can also use IWebElement objects.

注意:除了使用By.Id和By.ClassName,我们还可以使用IWebElement对象。

driver.FindElement(RelativeBy.WithTagName("a") .LeftOf(By.Id("search_query_top")) .Below(By.ClassName("icon-phone"))).Click();

Click on an element on the right of a known one

单击已知元素右侧的元素

This case shows the real power of relative locators. On the home page, we have the list of “popular best sellers”. In case we want to click on the second item in the list, simply call Relative Locators as shown below:

这种情况显示了相对定位器的真正力量。 在主页上,我们列出了“最受欢迎的畅销书”。 如果我们要单击列表中的第二项,只需调用相对定位器,如下所示:

driver.FindElement(RelativeBy.WithTagName("span") .RightOf(By.XPath("(//ul[@id='homefeatured']//img)[1]")) ).Click();

If we run the test, it will add the black blouse to the chart:

如果我们运行测试,它将在图表中添加黑色上衣:

What if screen resolution changes?

如果屏幕分辨率发生变化怎么办?

One question that popped up was: What if the screen resolution changes? Will it bring the test down? It is a real-life scenario, right? During the test creation process, we can use high-end monitors with high resolution and if we run it with some sort of task scheduler on a remote server — I guess, it could fail.

出现的一个问题是:如果屏幕分辨率发生变化怎么办? 它将使测试失败吗? 这是现实生活中的场景,对吧? 在测试创建过程中,我们可以使用高分辨率的高端监视器,如果我们在远程服务器上使用某种任务调度程序运行它,我可能会失败。

I am going to run the same test, in a debugging mode and stop on the line:

我将在调试模式下运行相同的测试,然后停止运行:

//try to click on the search button looking from the logo (it's on the right side)driver.FindElement(RelativeBy.WithTagName("button").RightOf(By.Id("header_logo"))).Click();

Now, I am going to resize the browser screen until the input field and the button goes under the logo.

现在,我将调整浏览器屏幕的大小,直到输入字段和按钮位于徽标下方。

Resized browser window
调整大小的浏览器窗口

Now, continue the test run!

现在,继续测试运行!

Test failed
测试失败

The test failed! That is good.

测试失败! 那很好。

The bad thing is that we should be very careful when it comes to responsive design.

不好的是,在进行响应式设计时,我们应该非常小心。

When will it go live?

它什么时候上线?

Well, back in 2018 Simon Stewart, the founding member of Selenium, had officially confirmed the release date and some of the major updates for Selenium 4 at the Selenium Conference in Bangalore.

好吧,早在2018年,Selenium的创始成员Simon Stewart在班加罗尔Selenium会议上就正式确认了Selenium 4的发布日期和一些重要更新。

The new (4.0) version of the Selenium was meant to be released by Christmas 2018. Currently, we are on the alpha 5 version so I guess it will be released in a year from now. There are still some bugs that are about to be fixed. Until released, we can play with alpha versions and prepare for an interesting future.

Selenium的新(4.0)版本本应在2018年圣诞节之前发布。目前,我们使用的是alpha 5版本,所以我想它将在一年后发布。 还有一些错误将要修复。 在发布之前,我们可以使用Alpha版本并为有趣的未来做准备。

Is this a game-changer?

这是改变游戏规则的人吗?

I do not think so. It is a cool concept of having a possibility to fetch an element in a different way, especially for those with dynamic attributes or even those with just one attribute. In the end, I do not see loads of locators fetched that way in the near future.

我不这么认为。 这是一个很酷的概念,它有可能以一种不同的方式来获取元素,尤其是对于那些具有动态属性的元素甚至是只有一个属性的元素。 最后,我看不到在不久的将来会以这种方式获取定位器的负载。

However, Selenium 4 has other cool stuff that can make our life easier and I’ll try to cover those as well.

但是,Selenium 4还有其他一些很酷的东西,可以使我们的生活更轻松,我也将尽力介绍这些东西。

Till the next time, happy testing!

直到下一次,祝您测试愉快!

翻译自: https://medium.com/maestral-solutions/a-sneak-peek-into-selenium-4-0-relative-locators-with-net-core-3-1-127e738dcb6b


http://www.taodudu.cc/news/show-2062024.html

相关文章:

  • oracle11g查询优化器,ORACLE中的优化器
  • Java递归下降分析器_递归下降语法分析器
  • mysql实体监听器_监听器模式(Listener)
  • 如何避免计算机被别人共享,win7如何防止别人偷窥电脑 win7防止别人偷窥电脑操作方法...
  • 偷窥JCache API(JSR 107)
  • 偷窥狂与暗物质
  • 计算机课禁用监视器,win7系统防止别人偷窥电脑的操作方法
  • 偷窥桌面程序和IE浏览器的密码编辑框
  • 偷窥Play Framework 2.0
  • 用DialogBox生成的对话框一开始就隐藏起来
  • DialogBoxParam(hinstExe, MAKEINTRESOURCE(IDD_ERRORSHOW), NULL, Dlg_Proc, _ttoi(pszCmdLine));
  • DialogBox不显示对话框的原因分析
  • 在Dialog中设置焦点失败?
  • DIALOGBOXPARAMA
  • 7.3 通用控件
  • 非模式对话框CreateDialog() 与 模式对话框 DialogBoxParam()和...
  • NativeWindow_02_DialogBoxParam_VC6
  • Windows核心编程学习一:使用DialogBoxParam显示模式对话框
  • (17)DialogBox和DialogBoxParam函数
  • C语言Dialogbox添加图片,c# dll c 类_dialogboxparam_msg结构
  • 向对话框传递数据DialogBoxParam;获取对话框返回的数据DialogBox;EndDialog;强制转换;
  • Windows API一日一练(17-18)DialogBox DialogBoxParam EndDialog函数
  • Windows API一日一练 17 DialogBox和DialogBoxParam函数
  • 【Win32】只此一篇 让你清楚明细模式(DialogBoxParam)与非模式(CreateDialogParam)对话框的区别
  • DialogBox和DialogBoxParam函数
  • 跟我一起玩Win32开发(18):使用对话框的两个技巧
  • 非模式对话框CreateDialog() 与 模式对话框 DialogBoxParam()和DialogBox()
  • Windows API一日一练(17)DialogBox和DialogBoxParam函数
  • DialogBoxParam()在动态库调用中创建模式对话框
  • C语言编译器哪个好用,常用C语言编译器有哪些

偷窥Selenium4 0带有网芯的相对定位器3 1相关推荐

  1. Selenium4.0+Python手撸自动化框架系列之 Web元素等待方式介绍 与 封装

    目录 前言 三种等待 一.线程等待 二.隐性等待 三.显性等待 封装 一.参数设计 二.函数名设计 三.封装代码设计 前言 web自动化测试,常常因为硬件配制,浏览器,网速等因素导致网页加载速度过慢, ...

  2. android wear 2.0 访问网络,离线AI使Android Wear 2.0断网也智能回复

    离线AI使Android Wear 2.0断网也智能回复 2017年02月13日 09:21作者:李佳辉编辑:李佳辉文章出处:泡泡网原创 分享 日前,谷歌终于发布了姗姗来迟的 Android Wear ...

  3. yolov5-5.0训练模型+瑞芯微rv1126上实现模型部署

    yolov5-5.0训练模型+瑞芯微rv1126上实现模型部署   第一次接触模型训练和在开发板部署,过程曲折,从开始的一脸懵到最后模型部署成功,查阅了不少资料和学习了不少大佬的经验,在这里记录一下过 ...

  4. Clear Case V7.0 官网下载地址

    clear case 7.0官网下载地址改变 https://www14.software.ibm.com/webapp/iwm/web/reg/download.do?source=RATL-RAT ...

  5. harmonyOS2,Harmonyos系统下载|Harmonyos2.0官网 v2.0-520下载站

    Harmonyos2.0官网是一款功能极其超前的国产系统,Harmonyos2.0官网拥有极其完善和强大的功能设计,系统上主要为安全为中心,拥有极其强大的安全的内核设计,可以为用户提供极极佳的使用环境 ...

  6. 比特元BTY简介:老牌加密数字货币,3.0主网成功,新篇章开启!

    比特元是一种简单稳定.拓展性强的区块链网络. 比特元发行于2014年初,是老牌加密数字货币了,当年命名为马币,初期为POW版本,社区创始人深感POW的局限,2015年转为POS,并改名叫比特元,代币简 ...

  7. 鸿蒙官网首页关于2.0版本,鸿蒙2.0官网,鸿蒙2.0手游官网最新版预约 v2.0-手游汇...

    鸿蒙2.0官网是一款非常完美的仙侠冒险游戏,游戏中有很多丰富的地图场景等着你来探索,在冒险战斗过程中提升角色的等级,获得更多强大的技能,你将会一步步的走上巅峰,还有很多武器装备可以使用哦. 鸿蒙2.0 ...

  8. 优品股票通电脑客户端 v1.0官网最新版

    名称:优品股票通电脑客户端 v1.0官网最新版 版本:1.0 软件大小:1.1MB 软件语言:简体中文 软件授权:免费版 应用平台:WinXP/Win7/Win8/Win10/WinAll 优品股票通 ...

  9. android n刷机,Mate 8 N版本(EMUI5.0+Android7.0) B523网盘刷机成功

    本帖最后由 huafans01197729011 于 2016-11-30 13:11 编辑 PS:1.感谢各位大大分享!!! 2.据说B523是无法退回的,慎重刷,建议是不要刷!!!3.刷机会变砖头 ...

  10. Away3D 4.0官网教程(翻译)

    使用Away3D 4.Stage3D 创建3D游戏和应用程序 (此帖每天都会更新,一定让大家完全的搞明白) 补充区:        'vase.awd' 可以使用 Prefab3D打开(在帖子后面回复 ...

最新文章

  1. 装饰器的定义、语法糖用法及示例代码
  2. 通配符(WildCard)的使用
  3. Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸
  4. 小米手环无法模拟门卡_MIUI12轻体验:关于模拟门禁卡,你想知道的都在这里
  5. java redis释放连接_redis在应用中使用连接不释放问题解决
  6. 个基于TensorFlow的简单故事生成案例:带你了解LSTM
  7. 帝国CMS7.5响应式后台美化模板 支持GBK+UTF
  8. MySQL将一张表数据插入到另一张表
  9. sonar覆盖率怎么统计的_实战|Java 测试覆盖率 Jacoco插桩的不同形式总结和踩坑记录(上)...
  10. vue仿微博评论回复_vue-微博评论
  11. display:HDCP协议简述
  12. 百度统计接口调用——登录接口
  13. 胡润研究院首发中国元宇宙潜力企业榜,巨杉数据库入选未来之星企业
  14. 基于Vue.js活动倒计时组件
  15. Markdown花样表格一键生成-基于Python
  16. HDMI设计2----EDID and E-EDID
  17. java画满天星_上机题目(初级)- 绘制满天星(Java)
  18. 玉米油、橄榄油、茶油是对心脑血管最好的油
  19. 软考高级 真题 2010年下半年 信息系统项目管理师 论文
  20. 解决 java BufferedReader.readLine()方法按行读取文件内容中文乱码的问题

热门文章

  1. 考研心得--一个差劲的ACMer
  2. 浅谈设备驱动的作用与本质,有无操作系统Linux设备驱动的区别
  3. mysql8.0重置密码
  4. 网络管理之SNMP协议
  5. php压缩解压zip文件夹,php利用ZipArchive类实现文件压缩与解压
  6. 每天一个PS技巧(原理+实践)——简单背景的抠图与毛发抠图
  7. 汽车品牌如何运营用户?
  8. OpenCV——LBP特征
  9. 如果浏览器大战的格局改变会怎样?
  10. wget 下载 设置cookie