简体   繁体   English

如何使用 PROTRACTOR E2E 在应用程序的每个页面中重用和运行相同的测试用例?

[英]How to reuse and run same test cases in every page of application using PROTRACTOR E2E?

REQUIREMENT:要求:

  • For this example let us consider testing the look and feel of buttons through out the application based on my custom CSS styleguide对于这个示例,让我们考虑根据我的自定义 CSS 样式指南在整个应用程序中测试按钮的外观和感觉
  • I have written test cases to test all the states of buttons (eg. button color on hover, box shadow of buttons, css classes been used by buttons etc...)我编写了测试用例来测试按钮的所有状态(例如,hover 上的按钮颜色、按钮的框阴影、按钮使用的 css 类等......)
  • I want to reuse the same test cases to be used in all the pages of the application我想在应用程序的所有页面中重用相同的测试用例
  • The application is Angular 8 and E2E is PROTRACTOR应用程序是 Angular 8 和 E2E 是 PROTRACTOR

WHAT I'VE TRIED:我试过的:

// login.e2e-spec.ts

import { browser, by } from 'protractor';
import { LoginPage } from '../../pages/login.po';

describe('LOGIN page', () => {
  let page: LoginPage;

  beforeEach(async () => {
    page = new LoginPage();
    await page.navigateTo();
  });

  describe('Login form', async () => {
    it('should navigate to page containing login form', async () => {
      await expect(browser.getCurrentUrl()).toEqual(
        'http://localhost:4200/#/login'
      );
    });
    it('buttons suite', async () => {
      const buttons = await page.buttons.getAllButtonsByTag();
      page.buttons.testButtonsClasses(buttons);
    });
  });
});

//login.po.ts

import { Page } from '../classes/page';

export class LoginPage extends Page {
  public constructor() {
    super('login');
  }
}
//page.ts

import { browser } from 'protractor';
import { Buttons } from './buttons';
import { Url } from './url';

export class Page {
  public url: Url;
  public buttons: Buttons;

  public constructor(pageId) {
    this.url = new Url(pageId);
    this.buttons = new Buttons();
  }

  public async navigateTo(endpoint?) {
    const url = this.url['fullUrl'] + endpoint ? `/${endpoint}` : '';
    await browser.get(url);
  }
}
//buttons.ts

import { by, element } from 'protractor';
export class Buttons {
  public constructor() {}
  public getAllButtonsByTag() {
    return element.all(by.tagName('button'));
  }
  public async testButtonsClasses(buttons) {
    for (let i = 0; i < buttons.length; i++) {
      const classAttribute = await buttons[i].getAttribute('class');
      expect(classAttribute).toContain('btn');
    }
  }
}
  • In the above snaps, you can see the Logins page spec contains page specific specs.在上面的快照中,您可以看到登录页面规范包含页面特定规范。
  • Im trying to use a class based structure to reuse the BUTTON's test cases.我试图使用基于 class 的结构来重用 BUTTON 的测试用例。
  • But this is not working as expected.但这并没有按预期工作。 The result is given below....结果如下所示......
  • The buttons cases are not runned and its always passing the parent spec.按钮案例未运行,并且始终通过父规范。

报告

  • Debugged using breakpoints.使用断点调试。 Based on the screenshot the test should fail.根据屏幕截图,测试应该失败。 But its still passing.但它仍然过去了。

QUESTION:问题:

Can someone assist in solving this issue.有人可以协助解决这个问题。 I'm trying to reuse the same test cases in every page.我试图在每个页面中重用相同的测试用例。

在此处输入图像描述

the question is too broad.这个问题太宽泛了。 First shot in the dark would be - you need to use this在黑暗中的第一枪将是 - 你需要使用这个

await buttons.get(i).getAttribute('class')

instead of this而不是这个

await buttons[i].getAttribute('class')

you have a method你有一个方法

public async testButtonsClasses(buttons) {
    for (let i = 0; i < buttons.length; i++) {
      const classAttribute = await buttons[i].getAttribute('class');
      expect(classAttribute).toContain('btn');
    }
  }

First of all use .get(i) instead of [i] and I mentioned above.首先使用.get(i)而不是[i]和我上面提到的。 Then the method is asynchronous.然后该方法是异步的。 When something is async, it returns a promise.当某些东西是异步的,它返回一个 promise。 Any promise needs to be resolved.任何 promise 都需要解决。 The best way to resolve protractor's promises is to use await when you call the method解决量角器承诺的最好方法是在调用方法时使用await

await page.buttons.testButtonsClasses(buttons);

additionally, expect is synchronous, and doesn't need await .另外, expect是同步的,不需要await What you pass as a parameter returns a promise and DOES need await您作为参数传递的内容返回 promise 并且需要await

await expect(await browser.getCurrentUrl()).toEqual('http://localhost:4200/#/login');

Lastly, I think you're on the right track for externalizing your tests, but keep in mind it is a bad practice to put assertions in page objects.最后,我认为您在外部化测试方面处于正确的轨道上,但请记住,将断言放在页面对象中是一种不好的做法。 I can't name a good reason why from the top oh my head, but better to avoid it我无法从头顶上说出一个很好的理由,但最好避免它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用任务计划程序运行量角器e2e测试脚本– Windows 10 - Run Protractor e2e Test script Using Task Scheduler – Windows 10 为什么在使用带有 waitForAngularEnabled(true) 的量角器运行 e2e 测试时出现错误 - Why I get an error when I run an e2e test using protractor with waitForAngularEnabled(true) 使用量角器e2e测试来测量代码覆盖率Java - Measuring code coverage java using protractor e2e test ng e2e 使一些使用 protractor protractor.conf 命令成功的测试用例失败 - ng e2e fails some of the test cases which are successful with protractor protractor.conf command E2E如何在没有角度的情况下在其他网站上使用量角器测试? - How E2E test with protractor at other website without angular? e2e 如何在角度量角器中测试登录成功案例 - e2e how to test login sucess case in angular protractor 如何在e2e测试中使用Protractor.addMockModule - How to use Protractor.addMockModule in the e2e test 使用量角器在测试 E2E 中上传文件 - Upload file in test E2E with Protractor 如何为 highcharts-angular 编写 e2e 测试用例 - How to write e2e test cases for highcharts-angular 如何使用 protractor 在 e2e 测试中检查小吃店的可见性并单击操作? - How to check visibility of snack bar and click on action in e2e test using protractor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM