量角器测试用例大于100,但从html传递字符串

时间:2018-09-28 13:59:19

标签: javascript typescript jasmine protractor karma-jasmine

如何在量角器中将字符串更改为整数。我们无法知道问题所在,有时会给出正确的结果,有时会给出错误的结果。需要帮助...谢谢

app.e2e-spec.ts:

import {AppPage} from "./app.po";
import { saveAs } from 'file-saver/FileSaver';
import {browser, by, element, protractor} from "protractor";


describe('The Right Doctor Test Report ', function() {
    let page: AppPage;
    browser.waitForAngularEnabled(false);

    let linksArray:string[]=[];
    let imagesArray:string[]=[];
    let videosArray:string[]=[];
    let socialArray:string[]=[];
    beforeEach(() => {
        page = new AppPage();
    });

    it('Get All Home Page Test ', () => {
       /* page.navigateTo();*/
        browser.get('http://localhost:4200/');
        browser.driver.manage().window().maximize();
        browser.sleep(10000);
        //find all links
        let linkCount = element.all(by.css('.testChecking'));
        //here you click each of the links:
        linkCount.each(function(elem,index){
            elem.getAttribute('href').then(function(link){
                if(link!=null)
                    linksArray.push(link);
            });
        });
    });

        it('Single Video Page Test Cases: ', () => {
            for(let i=0;i<linksArray.length;i++) {
                browser.get(linksArray[i]).then(()=> {
                    // total views should be greater than 100
//expect(element(by.css('.sviews')).getText()).not.toBeLessThan(100);
expect(parseInt(element(by.css('.sviews')).getText())).not.toBeLessThan(100);
                });
                browser.sleep(1000);
            }
        });

});

protractor.conf.js

// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts

const { SpecReporter } = require('jasmine-spec-reporter');
var Jasmine2HtmlReporter = require('protractor-jasmine2-html-reporter');
exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome',
    // For Travis CI only
    chromeOptions: {
      binary: process.env.CHROME_BIN,
      args: ['--no-sandbox']
    }
  },
  directConnect: true,
  baseUrl: 'http://localhost:4200/',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 300000,
    print: function() {}
  },
  onPrepare: function() {
    jasmine.getEnv().addReporter(
        new Jasmine2HtmlReporter({
          savePath: './screenshots/'
        })
    );
    jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
      savePath: './screenshots/',
      screenshotsFolder: 'images',
      takeScreenshots: true,
      consolidateAll: true,
      cleanDestination: true,
      //takeScreenshotsOnlyOnFailures: true,
      //showPassed: false,
      fileName: 'TestReport'
    }));
    require('ts-node').register({
      project: 'e2e/tsconfig.e2e.json'
    });
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

如果我不使用parseInt,则:

53次观看和111次观看 enter image description here

40次观看和200次观看 enter image description here

111次观看和47次观看 enter image description here

40次观看和50次观看 enter image description here

如果我编写parseInt,则显示以下结果。 输出:

138次观看和340次观看 enter image description here

47次观看和340次观看 enter image description here

2 个答案:

答案 0 :(得分:1)

您期望.getText()返回一个字符串。它实际上返回了需要解决的承诺。在测试之前使用await来解决承诺,应该会给您所需的结果。

let str = await element(by.css('.sviews')).getText();
let num = parseInt(str);
await expect(num).not.toBeLessThan(100);

另一个可行的方法是检查长度而不是数字。小于100的任何数字都将是1或2位数字,而大于100的任何数字将是3位或更多数字。仅当字符串3个或更多字符时,正则表达式字符串.{3,}才会通过。

expect(element(by.css('.sviews')).getText()).toMatch(/.{3,}/);

答案 1 :(得分:0)

如果您确定您的字符串将始终为整数形式,则可以使用unary plus运算符而不是parseInt来简化代码。您可以这样做

element(by.css('.sviews')).getText()
    .then(function(num) {
        expect(+num).not.toBeLessThan(100);
    });