【问题标题】:How to write a simple e2e test?如何编写一个简单的 e2e 测试?
【发布时间】:2018-04-03 09:21:50
【问题描述】:

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

【问题讨论】:

    标签: angular typescript protractor e2e-testing endly


    【解决方案1】:

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

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

    【讨论】:

    • 这个错误意味着url没有改变并且仍然在同一页面,当测试运行时你看到页面改变了吗?
    • 你的目标网址是什么?它在另一个端口和应用程序上?
    • @"Samuel Liew" 免责声明,我为最终开源做出了贡献
    【解决方案2】:

    如何使用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
    

    【讨论】: