繁体   English   中英

量角器和WebDriver / Selenium中的Javascript错误

[英]Javascript Errors in Protractor and WebDriver / Selenium

我有一个Angular网站,它加载( https:// xycom / )并立即重定向到( https:// xycom /#/ )[作为示例]我最初的解决方案是按照其说明加载Protractor并运行它从那里。 但是,在失败之后,我从selenium网站上学习了基本的selenium webdriver教程,并尝试也运行它。 发生了相同的错误,因此我只能假定这是WebDriver问题。

我的量角器脚本非常简单:

登录说明:

import {browser, element, by, ExpectedConditions} from 'protractor';
import {get} from "selenium-webdriver/http";
describe('login NOTTHISONE, ##site_location## / ##username## / ##password##', function(){
    it('should login and get a token, then be able to see the site.', function(){
        var EC = ExpectedConditions;
        let siteLocation = '##site_location##';

        browser.waitForAngularEnabled(false);
        browser.get(siteLocation);
        blowser.sleep(60000);
    });
});

protractor.conf.js:

const { SpecReporter } = require('jasmine-spec-reporter');

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './src/**/*.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: true,
  baseUrl: 'AWRONGURL',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  },
  onPrepare() {
    require('ts-node').register({
      project: require('path').join(__dirname, './tsconfig.e2e.json')
    });
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  }
};

页面的体系结构使得在加载有角度的应用程序时显示加载屏幕(非角度)。 然后,一旦加载页面,便启用了角度站点。

问题是,当我在量角器中加载网站时,出现Javascript错误,如下所示:

ERROR TypeError: Cannot read property 'sendMessage' of undefined
    at new l (main.385b4b24313957c18fd6.js:1)
    at _a (vendor.9256f6b56fd7db28f00b.js:1)
    at va (vendor.9256f6b56fd7db28f00b.js:1)
    at Ja (vendor.9256f6b56fd7db28f00b.js:1)
    at za (vendor.9256f6b56fd7db28f00b.js:1)
    at Object.hs [as createRootView] (vendor.9256f6b56fd7db28f00b.js:1)
    at e.create (vendor.9256f6b56fd7db28f00b.js:1)
    at e.create (vendor.9256f6b56fd7db28f00b.js:1)
    at t.bootstrap (vendor.9256f6b56fd7db28f00b.js:1)
    at vendor.9256f6b56fd7db28f00b.js:1

sendMessage是我的Angular应用程序中的一个函数。 所以有些事情....奇怪的是。 当我将waitForAngularEnabled更改为true时,Angular会告诉我我没有Angular页面(由于正在加载屏幕),但是当我将其更改为false时,它将导致此错误。 我应该在加载页面中添加一些内容以使量角器平静下来吗? 是否有一个简单的解决方案,不涉及设置一系列的回调?

但是,问题是:这与我尝试对远程服务器运行Protractor有关系吗? 我假设自从通过Web浏览器运行Protractor以来,我就可以将它指向我喜欢的任何服务器。 如果不是这种情况,则可能是为什么找不到JavaScript的原因。

编辑包括我的Python脚本:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create a new instance of the Chrome driver
driver = webdriver.Chrome(executable_path=r'C:\Program Files\chromedriver\chromedriver.exe')
# go to the google home page
driver.get("https://my.testsite.com")
# the page is ajaxy so the title is originally this:
print (driver.title)
# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_id("username")
# type in the search
inputElement.send_keys("cheese!")
# submit the form (although google automatically searches now without submitting)
inputElement.submit()
try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))
    # You should see "cheese! - Google Search"
    print (driver.title)
finally:
    driver.quit()

将您的登录语句放入browser.wait函数中:

browser.wait(EC.visibilityOf(element(by.id("mat-input3"))),60000).then(() => {
  browser.driver.findElement(by.id('mat-input3')).sendKeys(username);
  browser.driver.findElement(by.id('mat-input4')).sendKeys(password);
  browser.driver.findElement(by.xpath("//*[contains(text(), 'SIGN IN')]")).click();
});

好的,所以我的方法似乎有两点错误。 一位同事仔细检查并重新构建了所有内容:

  1. 我试图在不具有Selenium WebDriver服务器的远程服务器上运行Protractor。 这是一个错误,尽管我对一开始需要这样做并不满意。

  2. 我没有在protractor.conf.js设置中使用'jasmine2',显然,这对于他将我的角度项目组合在一起的方式是必需的。

只是把它放在那里,以防它对其他人有帮助。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM