Protractor和WebDriver/Selenium中的Javascript错误



我有一个Angular站点(https://x.y.com/)并立即重定向到(https://x.y.com/#/)[举个例子]我最初的解决方案是按照Protractor的说明加载它,然后从那里运行它。然而,在失败之后,我从selenium网站上学习了selenium网络驱动程序的基本教程,并尝试运行它。同样的错误也发生了,所以我只能假设这是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);
});
});

dractor.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 } }));
}
};

页面的架构是这样的,即在加载angular应用程序时显示加载屏幕(非angular)。然后,一旦加载页面,就会启用有角度的站点。

问题是,当我在Protractor中加载网站时,我会收到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平静下来吗?有没有一个简单的解决方案不需要设置一长串回调?

不过,问题是:这与我试图在远程服务器上运行Protractor有关吗?我以为,既然我通过网络浏览器运行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 Fileschromedriverchromedriver.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. 我在dractor.conf.js设置中没有使用"jasmine2",显然这对他组合我的角度项目是必要的。

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

最新更新