如何写一个简单的端到端测试?

How to write a simple e2e test?

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();
  }
}

这是我的页面url:

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

这是一个错误:

Expected 'http://localhost:49152/#/home' to contain 'account/subscriptions'.

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

https://monosnap.com/file/spODdEUjya4nylnLdh8pfzRJ9YxQgN

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

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');
      });
  });
});

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

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