Trying to check the value of this element on an angular plunker:
it('should display valid',function(){
browser.get('http://embed.plnkr.co/3nt01z/preview');
var errortext =element(By.xpath('body>div>h2>span:nth-child2)')).getText);
expect(errortext).toBe('Not:(');
})
Getting an error though:
Stacktrace:
InvalidSelectorError: invalid selector: Unable to locate an element with the xpath expression body>div>h2>span:nth-child(2) because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'body>div>h2>span:nth-child(2)' is not a valid XPath expression.
Is there a better way to find this span element or what is the right xpath expression?
I think you meant By.cssSelector(...).
By.xpath expects an XPath expression, which would be something like /body/div/h2/span[2], although I'm not sure about the exact syntax.
By the way, your code would be easier to test if you added an ID or CSS class to the element, so you don't need to depend on the exact DOM structure.
Try to use element(by.css('some css selector'))
Also, you have a typo in code - getText; instead of getText();
Or try this:
var errortext = element(By.xpath('/body/div/h2/span[2]')).getText();
Related
I am writing tests for React code where I get the error
Unable to find an element by: [data-testid="primary-cta"]
I actually want this to be for negative test case where the button is not displayed in the page
const renewButton = getByTestId('primary-cta');
expect(renewButton).not.toBeInTheDocument();
So I am using "not.toBeInTheDocument();" , but I am getting the error
Can anyone pleaase help ?
Thanks in advance
You can use queryByTestId if the element may not be present. queryBy* methods return null if the element is not found.
getBy* methods throw errors in case there are no matching elements. Hence, the error you are getting.
Relevant docs: Link
I'm currently writing some Protractor tests for an app. But in the app i'm using controller as syntax as the pieces I want to work on are components. The problem is that when i use the selector of "" $ctrl.functionName"" it's giving me an illegal selector statement.
Anyone have any ideas about this?
let btn = $$("button[ng-click^=$ctrl.initRequest].secondary.bf-btn");
expect(btn.isDisplayed()).toBe(true);
The error message is
Failed: invalid selector: An invalid or illegal selector was specified
If you cannot assign a meaningful id or other attribute to the element, you need to fix your current selector. At the very least, there should be quotes around the ng-click value:
button[ng-click^='$ctrl.initRequest'].secondary.bf-btn
Note that a slightly better version, that would also not require quotes, would be to use a partial match:
button[ng-click*=initRequest].secondary.bf-btn
And, see if you can drop the questionable classes and have just:
button[ng-click*=initRequest]
I simplified the code i need to test to this:
<html ng-app="home" ng-strict-di=""><head>....
And i am running some protractor tests, i want to access the value of ng-app so i can compare and see which app is running in each page.
I have tried
var appName = element(by.xpath('/html/#ng-app'))
but it is not returning a usable promise or text i can compare with
appName.getText().then(function(name) {
expect(name).toBe('home')
});
But protractor complains:
InvalidSelectorError: invalid selector: The result of the xpath expression "/html/#ng-app" is: [object Attr]. It should be an element.
So i'm a bit baffled as how can i access my angular app name from protractor to test for app running independently of localization of labels.
Any insight into this enigma?
And it seems like magically, when you order your thoughts to formulate the question, the answer comes to you.
the trick is to get the html as element
var appNameHtml = element(by.xpath('/html'))
and then, in the test, get the ng-app attribute and use it:
appNameHtml.getAttribute('ng-app').then(function(value) {
expect(value).toBe('home');
});
And bingo, you can extract the app name.
Maybe this is a very basic question, but it was driving me insane :)
your answer will suffice I guess in this case. But just wanted to highlight a more generic approach in case ng-app may reside not only on html element.
var elementWithNgApp = element(by.css('*[ng-app]'))
And then wanted to add one more thing. You need not resolve the promise of getAttribute to do an expect on its value. Jasmine resolves the promise for you. You can have something like this
expect(elementWithNgApp.getAttribute('ng-app')).toBe('home');
I'm working on a project using AngularJS. What I'm trying to do is to write my method's definition on my controller and pass it as string to be execute on a click even in client side:
And here is an example of what I'm trying to do:
My controller:
$scope.myMethod = "add({'a':3,'b':4})";
add (o)
{
return o.a+o.b;
}
My HTML page:
<button ng-click={{ myMethod }} ></button>
But I keep getting the Error: [$parse:syntax].
Can you tell me what I'm doing wrong?
The binding will insert the whole string:
"content"
That is to say, it inserts " inline, and since attribute values are enclosed by " as well, the inserted " ends the value expression prematurely. The rendered HTML looks like this:
ng-click=""add({'a':3,'b':4})""
Guard the binding with ', like this:
ng-click="'{{myMethod}}'"
I don't agree with the way you're inserting a method into the HTML, nor am I sure it works, but the above will fix the $parse error.
I have this tag in my app
<body id="ctl00_ctl00_Body" menuopen="true">
I'm trying expect if the menu is true or false. Right now I've tried
var body = element(by.id('ctl00_ctl00_Body'));
expect(body.getCssValue('menuopen').toEqual('true'))
and getting the error
TypeError: Object [object Object] has no method 'toEqual'
Thanks for any help! I am still very new at this. Google'd for the last few days and couldn't find anything
A bracket is misplaced in your code.
expect(body.getCssValue('menuopen')).toEqual('true')
From jasmine documentation:
Expectations are built with the function expect which takes a value, called the actual. It is chained with a Matcher function, which takes the expected value.
In your case the matcher is toEqual
Answering the comment:
Getting this error now UnknownError: unknown error: failed to parse
value of GET_EFFECTIVE_STYLE
menuopen is not a css value but an element attribute. So you have to use getAttribute instead of getCssValue
expect(body.getAttribute('menuopen')).toEqual('true')