微软自动化框架Playwright官方文档中的代码展示
  5fANJqpysGEA 2023年11月02日 19 0

今天我们来看下微软自动化框架Playwright官方文档中的代码展示:

  

Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met. Playwright comes with auto-wait built in meaning it waits for elements to be actionable prior to performing actions. Playwright provides assertThat overloads to write assertions.

Take a look at the example test below to see how to write a test using web first assertions, locators and selectors.


官方文档中举了个例子,用到了创建Playwright, 再创建一个浏览器,定义页面,新打开一个页面,找到元素。和之前我写的Demo差不多,但是官方的代码更规范。接下来我们看看对应的代码:


package org.example;import com.microsoft.playwright.*;import com.microsoft.playwright.options.AriaRole;import java.util.regex.Pattern;import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;public class NewPlaywrightDemo {

   public static void main(String[] args){
       //创建Playwright       try(Playwright playwright = Playwright.create()) {
           //新建浏览器           Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
                   .setHeadless(false));           //创建新页面           Page page = browser.newPage();           //导航到要测试验证的页面           page.navigate("http://playwright.dev");           Thread.sleep(30000);           //期望标题包含一个 子字符串           assertThat(page).hasTitle(Pattern.compile("Playwright"));           //创建一个锚点           Locator getStarted = page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions()
                   .setName("Get Started"));           //期望等待一个属性           assertThat(getStarted).hasAttribute("href","/docs/intro");           //点击要开始的链接           getStarted.click();           //           assertThat(page).hasURL(Pattern.compile(".*intro"));       catch (Exception e){
           e.printStackTrace();       }
   }
}

接下来介绍的是 判断

Playwright provides assertThat overloads which will wait until the expected condition is met.

assertThat(page).hasTitle(Pattern.compile("Playwright"));

定位 Locators

Locator getStarted = page.locator("text=Get Started");

assertThat(getStarted).hasAttribute("href", "/docs/intro");

getStarted.click();


Playwright supports many different locators like role text, test id 

and many more. Learn more about available locators

and how to pick one in this in-depth guide.


assertThat(page.locator("text=Installation")).isVisible();


今天先到这里,明天继续

微软自动化框架Playwright官方文档中的代码展示_microsoft

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  YgmmQQ65rPv4   2023年11月19日   13   0   0 Java应用程序
  Y8XIq1u6ceQW   2023年11月19日   25   0   0 Java
  AeUHztwqqxTz   2023年11月02日   22   0   0 Javatomcatapache
5fANJqpysGEA
最新推荐 更多