如何编写简单的e2e测试?

时间:2018-04-03 09:21:51

标签: angular typescript protractor e2e-testing endly

e2e测试应该转到特定页面并检查URL。

app.e2e-spec.ts

import { AppPageObject } from './app.po';

describe('Subscriptions', () => {
  let main: AppPageObject;

  beforeEach(() => {
    main = new AppPageObject();
  });

  it('should work', () => {
      main.navigateToSubscriptions().then(() => {
        main.getUrl().then(url => {
          expect(url).toContain('account/subscriptions');
        });
      });
  });
});

app.po.ts

import { browser, element, by } from 'protractor';

export class AppPageObject {
  navigateToSubscriptions() {
    return browser.get('/account/subscriptions');
  }

  getUrl() {
    return browser.getCurrentUrl();
  }
}

这是我的网页网址:

  

http://localhost:5001/#/account/subscriptions

这是一个错误:

  

预期' http://localhost:49152/#/home'包含'帐户/订阅'。

应该很容易。我做错了什么?

https://monosnap.com/file/spODdEUjya4nylnLdh8pfzRJ9YxQgN

2 个答案:

答案 0 :(得分:2)

也许您忘记了授权流程。看看这个例子:

app.po.ts:

import { browser, element, by } from 'protractor';

export class AppPageObject {
  public navigateTo(url?: string) {
    browser.get(url || '/');
  }

  public loginForm(email: string, password: string) {
    element(by.css("input[formcontrolname='email']")).sendKeys(email);
    element(by.css("input[formcontrolname='password']")).sendKeys(password);
    return element.all(by.id('login-button')).click();
  }

  public getCurrentUrl() {
    return browser.getCurrentUrl();
  }
}

app.e2e-spec.ts:

import { AppPageObject } from './app.po';
import { browser } from 'protractor';

describe('Subscriptions', () => {
  let main: AppPageObject;

  beforeEach(async () => {
    main = new AppPageObject;
    main.navigateTo('/#/auth/login');
  });

  it('should login', async () => {
    main.loginForm('test@gmail.com', '1234')
      .then(() => {
        browser.driver.sleep(6000);
      })
      .then(() => {
        expect(main.getCurrentUrl()).toContain('account/dashboard');
      });
  });
});

答案 1 :(得分:1)

如何将endly用作e2e集成,您可以使用以下命令运行硒测试

endly -r = test

@ test.yaml

defaults:
  target:
     URL: ssh://127.0.0.1/
     credentials: localhost
pipeline:
  init:
    action: selenium:start
    version: 3.4.0
    port: 8085
    sdk: jdk
    sdkVersion: 1.8
  test:
    action: selenium:run
    browser: firefox
    remoteSelenium:
      URL: http://127.0.0.1:8085
    commands:
      - get(http://localhost:5001/#/account/subscriptions)
      - (xpath:input[formcontrolname='email']).clear
      - (xpath:input[formcontrolname='email']).sendKeys(email);
      - (xpath:input[formcontrolname='password']).clear
      - (xpath:input[formcontrolname='password']).sendKeys(password);
      - (#submit).click
      - command: CurrentURL = CurrentURL()
        exit: $CurrentURL:/account/subscriptions/
        sleepTimeMs: 1000
        repeat: 10
expect:
  CurrentURL: account/subscriptions