Microsoft Edge WebDriver:这是你应该知道的

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / 1v1 提问 / Java 学习路线 / 学习打卡 / 每月赠书 / 社群讨论

  • 新项目:《从零手撸:仿小红书(微服务架构)》 正在持续爆肝中,基于 Spring Cloud Alibaba + Spring Boot 3.x + JDK 17...点击查看项目介绍 ;
  • 《从零手撸:前后端分离博客项目(全栈开发)》 2 期已完结,演示链接: http://116.62.199.48/ ;

截止目前, 星球 内专栏累计输出 63w+ 字,讲解图 2808+ 张,还在持续爆肝中.. 后续还会上新更多项目,目标是将 Java 领域典型的项目都整一波,如秒杀系统, 在线商城, IM 即时通讯,权限管理,Spring Cloud Alibaba 微服务等等,已有 2200+ 小伙伴加入学习 ,欢迎点击围观

您可能知道,用于创建 Web 自动化测试的最佳可用框架之一是 selenium webdriver 。微软公开 宣布 他们新的 windows 10 web browser-edge 将支持 webdriver 自动化( microsoft edge webdriver )。当然,我想尽可能快地尝试一下,所以我准备了所需的一切并创建了几个测试。我将向您展示他们的源代码和结果。

到目前为止在“webdriver”系列中

1. 10分钟入门webdriver c#

2. 最被低估的 webdriver 定位器——xpath

3. webdriver selenium tor集成c#代码

4. 使用 webdriver 和 httpwebrequest 测试 url 重定向

5. microsoft edge webdriver——每个人都应该知道的

6. 通过 ram 事实和神话加速 selenium 测试

创建你的第一个 webdriver 测试项目

1.在visual studio中新建测试项目。

2. 安装 nuget 包管理器并导航到它。

3.搜索 selenium 安装 结果列表中的第一项

微软边缘 webdriver c# 代码

测试的测试用例

以下测试的主要目标是通过 me- diet generator 页面专门设计的“ 健康 ”饮食菜单。

firefox webdriver 中的代码示例

使用 firefox webdriver 将上述表格自动化是一项微不足道的任务。


 [testclass]
public class healthydietmenugeneratortestsfirefox
{
    private iwebdriver driver { get; set; }
[testinitialize]
public void setuptest()
{
    this.driver = new firefoxdriver();
}

[testcleanup]
public void teardowntest()
{
    this.driver.quit();
}

[testmethod]
public void fillawsomediettest()
{
    this.driver.navigate().gotourl(@"http://automatetheplanet.com/healthy-diet-menu-generator/");
    var addadditionalsugarcheckbox = this.driver.findelement(by.id("ninja_forms_field_18"));
    addadditionalsugarcheckbox.click();
    var venticoffeeradiobutton = this.driver.findelement(by.id("ninja_forms_field_19_1"));
    venticoffeeradiobutton.click();
    selectelement selectelement = new selectelement(this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_21']")));
    selectelement.selectbytext("7 x bbq ranch burgers");
    var smotheredchocolatecakecheckbox = this.driver.findelement(by.id("ninja_forms_field_27_2"));
    smotheredchocolatecakecheckbox.click();
    var addsomethingtodiettextarea = this.driver.findelement(by.id("ninja_forms_field_22"));
    addsomethingtodiettextarea.sendkeys(@"goi cuon- this snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. it’s “meat light,” with the flavors of refreshing herbs erupting in your mouth.");
    var rockstarrating = this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_20_div_wrap']/span/div[11]/a"));
    rockstarrating.click();
    var firstnametextbox = this.driver.findelement(by.id("ninja_forms_field_23"));
    firstnametextbox.sendkeys("anton");
    var lastnametextbox = this.driver.findelement(by.id("ninja_forms_field_24"));
    lastnametextbox.sendkeys("angelov");
    var emailtextbox = this.driver.findelement(by.id("ninja_forms_field_25"));
    emailtextbox.sendkeys("aangelov@yahoo.com");
    var awsomedietsubmitbutton = this.driver.findelement(by.id("ninja_forms_field_28"));
    awsomedietsubmitbutton.click();
}

}

驱动程序实例在 testsetup 方法中创建并在 testcleanup 中处理。之后,测试导航到所需的页面,找到指定的元素并执行所需的操作。如果您不知道如何使用不同的 webdriver 方法,您可能会发现我关于这个问题的快速阅读教程很有趣 - getting started with webdriver c# in 10 minutes

测试执行了约 16 秒。

firefox webdriver + 页面对象中的代码示例

我还重写了测试以使用 webdriver 页面对象。我创建了一个 healthydietgeneratorpage 类,其中存在所有元素。


 [testclass]
public class healthydietmenugeneratortestsfirefox
{
    private iwebdriver driver { get; set; }
[testinitialize]
public void setuptest()
{
    this.driver = new firefoxdriver();
}

[testcleanup]
public void teardowntest()
{
    this.driver.quit();
}

[testmethod]
public void fillawsomediettest()
{
    this.driver.navigate().gotourl(@"http://automatetheplanet.com/healthy-diet-menu-generator/");
    var addadditionalsugarcheckbox = this.driver.findelement(by.id("ninja_forms_field_18"));
    addadditionalsugarcheckbox.click();
    var venticoffeeradiobutton = this.driver.findelement(by.id("ninja_forms_field_19_1"));
    venticoffeeradiobutton.click();
    selectelement selectelement = new selectelement(this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_21']")));
    selectelement.selectbytext("7 x bbq ranch burgers");
    var smotheredchocolatecakecheckbox = this.driver.findelement(by.id("ninja_forms_field_27_2"));
    smotheredchocolatecakecheckbox.click();
    var addsomethingtodiettextarea = this.driver.findelement(by.id("ninja_forms_field_22"));
    addsomethingtodiettextarea.sendkeys(@"goi cuon- this snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. it’s “meat light,” with the flavors of refreshing herbs erupting in your mouth.");
    var rockstarrating = this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_20_div_wrap']/span/div[11]/a"));
    rockstarrating.click();
    var firstnametextbox = this.driver.findelement(by.id("ninja_forms_field_23"));
    firstnametextbox.sendkeys("anton");
    var lastnametextbox = this.driver.findelement(by.id("ninja_forms_field_24"));
    lastnametextbox.sendkeys("angelov");
    var emailtextbox = this.driver.findelement(by.id("ninja_forms_field_25"));
    emailtextbox.sendkeys("aangelov@yahoo.com");
    var awsomedietsubmitbutton = this.driver.findelement(by.id("ninja_forms_field_28"));
    awsomedietsubmitbutton.click();
}

}

第一次创建对象时,所有元素都通过 webdriver 的 pagefactory 进行初始化。

测试代码几乎相同,唯一的区别是元素初始化现在由页面对象负责。


 [testclass]
public class healthydietmenugeneratortestsfirefox
{
    private iwebdriver driver { get; set; }
[testinitialize]
public void setuptest()
{
    this.driver = new firefoxdriver();
}

[testcleanup]
public void teardowntest()
{
    this.driver.quit();
}

[testmethod]
public void fillawsomediettest()
{
    this.driver.navigate().gotourl(@"http://automatetheplanet.com/healthy-diet-menu-generator/");
    var addadditionalsugarcheckbox = this.driver.findelement(by.id("ninja_forms_field_18"));
    addadditionalsugarcheckbox.click();
    var venticoffeeradiobutton = this.driver.findelement(by.id("ninja_forms_field_19_1"));
    venticoffeeradiobutton.click();
    selectelement selectelement = new selectelement(this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_21']")));
    selectelement.selectbytext("7 x bbq ranch burgers");
    var smotheredchocolatecakecheckbox = this.driver.findelement(by.id("ninja_forms_field_27_2"));
    smotheredchocolatecakecheckbox.click();
    var addsomethingtodiettextarea = this.driver.findelement(by.id("ninja_forms_field_22"));
    addsomethingtodiettextarea.sendkeys(@"goi cuon- this snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. it’s “meat light,” with the flavors of refreshing herbs erupting in your mouth.");
    var rockstarrating = this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_20_div_wrap']/span/div[11]/a"));
    rockstarrating.click();
    var firstnametextbox = this.driver.findelement(by.id("ninja_forms_field_23"));
    firstnametextbox.sendkeys("anton");
    var lastnametextbox = this.driver.findelement(by.id("ninja_forms_field_24"));
    lastnametextbox.sendkeys("angelov");
    var emailtextbox = this.driver.findelement(by.id("ninja_forms_field_25"));
    emailtextbox.sendkeys("aangelov@yahoo.com");
    var awsomedietsubmitbutton = this.driver.findelement(by.id("ninja_forms_field_28"));
    awsomedietsubmitbutton.click();
}

}

执行时间几乎没有变化——大约 15 秒。反正快了一点。

Microsoft Edge 网络驱动程序先决条件

1. 从微软官方网站 下载 microsoft edge webdriver 可执行文件。

2. 从之前下载的安装程序中 安装 microsoft edge webdriver

3. 创建 一个虚拟机或将您的操作系统 升级 windows 10 ,microsoft edge webdriver 仅与其兼容。

Microsoft Edge webdriver 中的代码示例

理论上,相同的测试应该能够通过新的 webdriver 执行,只需要交换驱动程序类型。 microsoft edge webdriver 的设置稍微复杂一些。此外,还有一些尚未支持的方法,例如 xpath gotourl 和 findelement。导航到新的 url 是通过 this.driver.url 分配实现的。


 [testclass]
public class healthydietmenugeneratortestsfirefox
{
    private iwebdriver driver { get; set; }
[testinitialize]
public void setuptest()
{
    this.driver = new firefoxdriver();
}

[testcleanup]
public void teardowntest()
{
    this.driver.quit();
}

[testmethod]
public void fillawsomediettest()
{
    this.driver.navigate().gotourl(@"http://automatetheplanet.com/healthy-diet-menu-generator/");
    var addadditionalsugarcheckbox = this.driver.findelement(by.id("ninja_forms_field_18"));
    addadditionalsugarcheckbox.click();
    var venticoffeeradiobutton = this.driver.findelement(by.id("ninja_forms_field_19_1"));
    venticoffeeradiobutton.click();
    selectelement selectelement = new selectelement(this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_21']")));
    selectelement.selectbytext("7 x bbq ranch burgers");
    var smotheredchocolatecakecheckbox = this.driver.findelement(by.id("ninja_forms_field_27_2"));
    smotheredchocolatecakecheckbox.click();
    var addsomethingtodiettextarea = this.driver.findelement(by.id("ninja_forms_field_22"));
    addsomethingtodiettextarea.sendkeys(@"goi cuon- this snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. it’s “meat light,” with the flavors of refreshing herbs erupting in your mouth.");
    var rockstarrating = this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_20_div_wrap']/span/div[11]/a"));
    rockstarrating.click();
    var firstnametextbox = this.driver.findelement(by.id("ninja_forms_field_23"));
    firstnametextbox.sendkeys("anton");
    var lastnametextbox = this.driver.findelement(by.id("ninja_forms_field_24"));
    lastnametextbox.sendkeys("angelov");
    var emailtextbox = this.driver.findelement(by.id("ninja_forms_field_25"));
    emailtextbox.sendkeys("aangelov@yahoo.com");
    var awsomedietsubmitbutton = this.driver.findelement(by.id("ninja_forms_field_28"));
    awsomedietsubmitbutton.click();
}

}

使用 firefox 驱动程序执行的相同测试完成了 16 秒 ;现在它使用新的 Microsoft Edge webdriver 运行了 4 秒

页面对象的实现甚至更快。测试执行仅用了 3 秒。


 [testclass]
public class healthydietmenugeneratortestsfirefox
{
    private iwebdriver driver { get; set; }
[testinitialize]
public void setuptest()
{
    this.driver = new firefoxdriver();
}

[testcleanup]
public void teardowntest()
{
    this.driver.quit();
}

[testmethod]
public void fillawsomediettest()
{
    this.driver.navigate().gotourl(@"http://automatetheplanet.com/healthy-diet-menu-generator/");
    var addadditionalsugarcheckbox = this.driver.findelement(by.id("ninja_forms_field_18"));
    addadditionalsugarcheckbox.click();
    var venticoffeeradiobutton = this.driver.findelement(by.id("ninja_forms_field_19_1"));
    venticoffeeradiobutton.click();
    selectelement selectelement = new selectelement(this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_21']")));
    selectelement.selectbytext("7 x bbq ranch burgers");
    var smotheredchocolatecakecheckbox = this.driver.findelement(by.id("ninja_forms_field_27_2"));
    smotheredchocolatecakecheckbox.click();
    var addsomethingtodiettextarea = this.driver.findelement(by.id("ninja_forms_field_22"));
    addsomethingtodiettextarea.sendkeys(@"goi cuon- this snack made from pork, shrimp, herbs, rice vermicelli and other ingredients wrapped in rice paper is served at room temperature. it’s “meat light,” with the flavors of refreshing herbs erupting in your mouth.");
    var rockstarrating = this.driver.findelement(by.xpath("//*[@id='ninja_forms_field_20_div_wrap']/span/div[11]/a"));
    rockstarrating.click();
    var firstnametextbox = this.driver.findelement(by.id("ninja_forms_field_23"));
    firstnametextbox.sendkeys("anton");
    var lastnametextbox = this.driver.findelement(by.id("ninja_forms_field_24"));
    lastnametextbox.sendkeys("angelov");
    var emailtextbox = this.driver.findelement(by.id("ninja_forms_field_25"));
    emailtextbox.sendkeys("aangelov@yahoo.com");
    var awsomedietsubmitbutton = this.driver.findelement(by.id("ninja_forms_field_28"));
    awsomedietsubmitbutton.click();
}

}

源代码

您可以从我的 github 存储库下载完整的源代码。

如果您喜欢我的出版物,请随时 订阅 http://automatetheplanet.com/newsletter/ 也可以点击这些分享按钮。 谢谢你!

相关文章