在 Protractor JS 中使用 "if statement" 时如何解决关于 "invalid selector" 的错误?
How to solve error about "invalid selector" when using "if statement" in Protractor JS?
我想使用一个不可见的元素作为条件,如果它存在做 X,否则做 Y。
我已经尝试使用这个元素的 class 作为定位器和 ng-model 以及他的绑定,但是 none 是可行的:/
请您协助解决这个问题,非常感谢。
这是我的有问题的测试用例:
describe('LiveSite Portal - Existing client send new message', function() {
var msgBox = expect(element(by.css(".final-container")).isPresent()).toBe(true);
//var msgBox = element.all(by.css('#main_container > main > div > div > section > div.row.cz-content.inner-content.full-height > div > div.action.ng-scope > footer > form > div.row.form-control.textarea-holder.ng-isolate-scope.ng-hide > textarea'));
var EC = protractor.ExpectedConditions;
it('LiveSite - Home Page', function() {
liveSiteHome();
});
if (msgBox === false) {
it('LiveSite Portal - Existing client send new message - Message form', function() {
browser.wait(EC.visibilityOf(element(by.xpath("//div[@class='actions-row']//a[.='Leave Your Details']"))), 10000);
element(by.xpath("//div[@class='actions-row']//a[.='Leave Your Details']")).click();
waitPageToLoad();
expect(element(by.id("new_client_message_message")).isPresent()).toBe(true);
element(by.css("div.row.mandatory > div.area")).click();
element(by.id("new_client_message_title")).sendKeys("Automation message");
element(by.id("new_client_message_message")).sendKeys("I am not logged-in client. Please call me.");
element(by.id("new_client_message_email")).sendKeys("idanvcita@gmail.com");
});
it('LiveSite Portal - Existing client send new message - Welcome back for existing client', function() {
element(by.id("new_client_message_first_name")).click();
browser.wait(EC.visibilityOf(element(by.css("span.welcome-back"))), 10000);
expect(element(by.css("span.welcome-back")).isPresent()).toBe(true);
});
it('LiveSite Portal - Existing client send new message - Messeage Sent', function() {
element(by.name("commit")).click();
waitPageToLoad();
expect(element(by.css(".final-container")).isPresen()).toBe(true);t
});
it('LiveSite Portal - Existing client send new message - Back to home page', function() {
element(by.css(".standard-button")).click();
expect(element(by.model("email")).isPresent()).toBe(true);
browser.driver.sleep(2000);
});
} else {
it('LiveSite Portal - Send new message from the text box - Text Area', function() {
// expect(element(msgBtn.isPresent()).toBe(true);
element(by.xpath("//div[@id='main_container']/main/div/div/section/div[2]/div/div[2]/footer/form/div[2]/textarea")).sendKeys("Hello!\nHow are you?\nHave a good day! :)");
});
it('LiveSite Portal - Send new message from the text box - Message has been sent', function() {
element(by.xpath("//div[@id='main_container']//button[.='Send']")).click();
browser.wait(EC.visibilityOf(element(by.xpath("//div[@id='main_container']//h2[.='Quick conversation']"))), 10000);
browser.driver.sleep(2000);
});
}
});
这是元素 = msgBox:
<a class="btn btn-void btn-with-font-round-icon engage-btn icon-lg brand-theme-before icon-env ng-binding" ng-click="goExternalUrl(contact_url)" ng-bind="truncate(livesite_actions_texts.contact, 22)" title="Leave Your Details">Leave Your Details</a>
这是错误:
c:\automation\tests>protractor conf.js
Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
LiveSite Portal - Existing client send new message
encountered a declaration exception - fail
Failures:
1) LiveSite Portal - Existing client send new message encountered a declaratio
n exception
Message:
TypeError: Cannot read property 'matchersClass' of null
Stacktrace:
TypeError: Cannot read property 'matchersClass' of null
at promiseMatchers (C:\Users\idan\AppData\Roaming\npm\node_modules\protracto
r\node_modules\jasminewd\index.js:203:38)
at global.expect (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\
node_modules\jasminewd\index.js:221:12)
at [object Object].<anonymous> (c:\automation\tests\msg.js:3:61)
at [object Object].jasmine.Env.describe_ (C:\Users\idan\AppData\Roaming\npm\
node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:913:21
)
at [object Object].jasmine.Env.describe (C:\Users\idan\AppData\Roaming\npm\n
ode_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:898:15)
at describe (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_
modules\minijasminenode\lib\jasmine-1.3.1.js:658:27)
at Object.<anonymous> (c:\automation\tests\msg.js:1:63)
Finished in 0.125 seconds
1 test, 1 assertion, 1 failure
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1
c:\automation\tests>
既然你说这个问题发生在 if
语句中,我假设这个问题发生在访问 msgBox 时。所以尝试使用 element.all(by.css('.ng-isolate-scope ng-pristine ng-valid)'));
而不是 element(by.css(....))
.
或者尝试像这样访问元素
var msgBox = element.all(by.css('[ng-click="goExternalUrl(contact_url)"]'));
这一行:
var msgBox = expect(element(by.css(".final-container")).isPresent()).toBe(true);
发生在任何 it
声明之外。但是所有 Protractor(真正的 webdriver)神奇的承诺解决方案都发生在每个 it
上创建的 ControlFlow 中。所以,我认为这不会像您预期的那样有效。
第二个:
if (msgBox === false) {
正在检查 expect
的结果,这是一个 Promise(如果有的话)。我非常有信心它不是布尔值。在任何情况下,您都在尝试更改将执行哪些 it
函数,但这需要执行一些量角器代码,这实际上不会起作用。
您似乎想要检查页面,确定它是哪个 "flavor",然后 运行 对不同风格(有或没有文本框)进行不同的测试。这似乎是一件合理的事情,但我认为 Protractor 没有很好的表达方式。 (我认为很多人会建议您进行设置,以便您的测试 知道 预期哪种情况,并且它会导致两组代码都执行。这种条件方法意味着测试 "passing" 可能没有练习你所有的代码。)
我想使用一个不可见的元素作为条件,如果它存在做 X,否则做 Y。
我已经尝试使用这个元素的 class 作为定位器和 ng-model 以及他的绑定,但是 none 是可行的:/
请您协助解决这个问题,非常感谢。
这是我的有问题的测试用例:
describe('LiveSite Portal - Existing client send new message', function() {
var msgBox = expect(element(by.css(".final-container")).isPresent()).toBe(true);
//var msgBox = element.all(by.css('#main_container > main > div > div > section > div.row.cz-content.inner-content.full-height > div > div.action.ng-scope > footer > form > div.row.form-control.textarea-holder.ng-isolate-scope.ng-hide > textarea'));
var EC = protractor.ExpectedConditions;
it('LiveSite - Home Page', function() {
liveSiteHome();
});
if (msgBox === false) {
it('LiveSite Portal - Existing client send new message - Message form', function() {
browser.wait(EC.visibilityOf(element(by.xpath("//div[@class='actions-row']//a[.='Leave Your Details']"))), 10000);
element(by.xpath("//div[@class='actions-row']//a[.='Leave Your Details']")).click();
waitPageToLoad();
expect(element(by.id("new_client_message_message")).isPresent()).toBe(true);
element(by.css("div.row.mandatory > div.area")).click();
element(by.id("new_client_message_title")).sendKeys("Automation message");
element(by.id("new_client_message_message")).sendKeys("I am not logged-in client. Please call me.");
element(by.id("new_client_message_email")).sendKeys("idanvcita@gmail.com");
});
it('LiveSite Portal - Existing client send new message - Welcome back for existing client', function() {
element(by.id("new_client_message_first_name")).click();
browser.wait(EC.visibilityOf(element(by.css("span.welcome-back"))), 10000);
expect(element(by.css("span.welcome-back")).isPresent()).toBe(true);
});
it('LiveSite Portal - Existing client send new message - Messeage Sent', function() {
element(by.name("commit")).click();
waitPageToLoad();
expect(element(by.css(".final-container")).isPresen()).toBe(true);t
});
it('LiveSite Portal - Existing client send new message - Back to home page', function() {
element(by.css(".standard-button")).click();
expect(element(by.model("email")).isPresent()).toBe(true);
browser.driver.sleep(2000);
});
} else {
it('LiveSite Portal - Send new message from the text box - Text Area', function() {
// expect(element(msgBtn.isPresent()).toBe(true);
element(by.xpath("//div[@id='main_container']/main/div/div/section/div[2]/div/div[2]/footer/form/div[2]/textarea")).sendKeys("Hello!\nHow are you?\nHave a good day! :)");
});
it('LiveSite Portal - Send new message from the text box - Message has been sent', function() {
element(by.xpath("//div[@id='main_container']//button[.='Send']")).click();
browser.wait(EC.visibilityOf(element(by.xpath("//div[@id='main_container']//h2[.='Quick conversation']"))), 10000);
browser.driver.sleep(2000);
});
}
});
这是元素 = msgBox:
<a class="btn btn-void btn-with-font-round-icon engage-btn icon-lg brand-theme-before icon-env ng-binding" ng-click="goExternalUrl(contact_url)" ng-bind="truncate(livesite_actions_texts.contact, 22)" title="Leave Your Details">Leave Your Details</a>
这是错误:
c:\automation\tests>protractor conf.js
Using the selenium server at http://localhost:4444/wd/hub
[launcher] Running 1 instances of WebDriver
LiveSite Portal - Existing client send new message
encountered a declaration exception - fail
Failures:
1) LiveSite Portal - Existing client send new message encountered a declaratio
n exception
Message:
TypeError: Cannot read property 'matchersClass' of null
Stacktrace:
TypeError: Cannot read property 'matchersClass' of null
at promiseMatchers (C:\Users\idan\AppData\Roaming\npm\node_modules\protracto
r\node_modules\jasminewd\index.js:203:38)
at global.expect (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\
node_modules\jasminewd\index.js:221:12)
at [object Object].<anonymous> (c:\automation\tests\msg.js:3:61)
at [object Object].jasmine.Env.describe_ (C:\Users\idan\AppData\Roaming\npm\
node_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:913:21
)
at [object Object].jasmine.Env.describe (C:\Users\idan\AppData\Roaming\npm\n
ode_modules\protractor\node_modules\minijasminenode\lib\jasmine-1.3.1.js:898:15)
at describe (C:\Users\idan\AppData\Roaming\npm\node_modules\protractor\node_
modules\minijasminenode\lib\jasmine-1.3.1.js:658:27)
at Object.<anonymous> (c:\automation\tests\msg.js:1:63)
Finished in 0.125 seconds
1 test, 1 assertion, 1 failure
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 failed 1 test(s)
[launcher] overall: 1 failed spec(s)
[launcher] Process exited with error code 1
c:\automation\tests>
既然你说这个问题发生在 if
语句中,我假设这个问题发生在访问 msgBox 时。所以尝试使用 element.all(by.css('.ng-isolate-scope ng-pristine ng-valid)'));
而不是 element(by.css(....))
.
或者尝试像这样访问元素
var msgBox = element.all(by.css('[ng-click="goExternalUrl(contact_url)"]'));
这一行:
var msgBox = expect(element(by.css(".final-container")).isPresent()).toBe(true);
发生在任何 it
声明之外。但是所有 Protractor(真正的 webdriver)神奇的承诺解决方案都发生在每个 it
上创建的 ControlFlow 中。所以,我认为这不会像您预期的那样有效。
第二个:
if (msgBox === false) {
正在检查 expect
的结果,这是一个 Promise(如果有的话)。我非常有信心它不是布尔值。在任何情况下,您都在尝试更改将执行哪些 it
函数,但这需要执行一些量角器代码,这实际上不会起作用。
您似乎想要检查页面,确定它是哪个 "flavor",然后 运行 对不同风格(有或没有文本框)进行不同的测试。这似乎是一件合理的事情,但我认为 Protractor 没有很好的表达方式。 (我认为很多人会建议您进行设置,以便您的测试 知道 预期哪种情况,并且它会导致两组代码都执行。这种条件方法意味着测试 "passing" 可能没有练习你所有的代码。)