gpt4 book ai didi

Protractor 和 WebDriver/Selenium 中的 Javascript 错误

转载 作者:行者123 更新时间:2023-12-03 00:29:43 25 4
gpt4 key购买 nike

我有一个 Angular 站点,它加载 ( https://x.y.com/ ) 并立即重定向到 ( https://x.y.com/#/ ) [作为示例] 我最初的解决方案是按照其说明加载 Protractor 并从那里运行它。然而,在此失败之后,我从 the selenium site 获取了基本的 selenium webdriver 教程。并尝试运行它。发生了同样的错误,所以我只能假设这是一个 WebDriver 问题。

我的 Protractor 脚本非常简单:

登录规范:

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

页面的架构是在加载 Angular 应用程序时显示加载屏幕(非 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 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();
});

关于Protractor 和 WebDriver/Selenium 中的 Javascript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53923435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com