Locator not found selenium - Javascript - javascript

I am trying to use a Javascript snippet to make sure two divs having some text co exist in another one [ Basically validate the existence of a comment on a certain post ]
The JS works on firebug but fails on selenium IDE.
javascript{
return ($.grep( $("#Posts li") .map(function(index,element){
if($(element).has(".puls_content:contains('post text')").length > 0){
if($(element).has("*:contains('this is a comment')").length > 0) {
return true
}
}
return false
}) , function(x){ return x}).length > 0) }
I get the Unexpected Exception error from selenium-api.js, any help ?

Selenium doesn't run javascript in the same context as firebug would. You are actually running from the selenium window outside the window you are testing. But never fear! They have thought of this. You will need to add something like
var jQuery=selenium.browserbot.getUserWindow().jQuery;
into your script and change all your $ to jQuery to get it to work the way you want it to. Check out this question and answer.
Secondly, I would suggest doing this with CSS or Xpath selectors and verifying the post and then the comment section separately. It would cause you to go through less hoops to figure out what you need.

Related

Delete empty tags when testing with Jmeter

I am setting up some tests for Jmeter but i've encountered a problem which i've difficulty solving. In my JMS Point-to-Point i am using the following in the content section in JMeter content
i have a csv file with testdata for testing. All variables are written in this CSV with their value. for instance CoverageNr has a value of 1 then during testing Jmeter will use "< CoverageNr>1< /CovereageNr>" if it doesn't have a value it is empty: "< CoverageNr>< /CovereageNr>". And here we have the issue which i have. CoverageNr cannot be empty when used. What i want to achieve is when CoverageNr has no value, the tag "< CoverageNr>< /CovereageNr>" is not included in the test. I guess i have to make the entire tag configurable or use an if controller but so far to no avail. Can somebody give me some insights how to solve this problem?
I heard Groovy is a new Black so you can do this as follows:
Add JSR223 PreProcessor as a child of the JMS Point to Point sampler
Put the following code into the "Script" area:
import groovy.xml.XmlUtil
def content = sampler.getContent()
Node xml = new XmlParser().parseText(content)
cleanNode(xml)
def newContent = XmlUtil.serialize(xml)
sampler.setContent(newContent)
boolean cleanNode(Node node) {
node.attributes().with { a ->
a.findAll { !it.value }.each { a.remove(it.key) }
}
node.children().with { kids ->
kids.findAll { it instanceof Node ? !cleanNode(it) : false }
.each { kids.remove(it) }
}
node.attributes() || node.children() || node.text()
}
Demo:
Source: Remove null attributes and empty children from Node
Going forward I would recommend migrating to JSR223 Test Elements from Beanshell so in terms of performance it would be much better. Moreover Groovy has some nice sexy features, i.e. aforementioned XML processing so it will make your life easier
So i've solved the issue thanks to the other questions mentioned in the comment above and thanks to Dmitri T's answers for those questions. using a Beanshell Pre Processor did the trick with script being:
String data = sampler.getContent();
data = data.replaceAll("<CoverageN></CoverageN>","");
sampler.setContent(data);
Maybe it can be usefull for others.

Why can't I use querySelector in protractor?

I am using protractor. I understand that protractor has Jquery like syntax but I need something that can
create conditions, variables and loops based on DOM elements in some of my testing. I want to be able to use querySelector. Using just promises won't let me do the kind of testing I need to do.
When I run it, it says:
Failed: Cannot read property 'querySelector' of undefined
or
Failed: document is not defined
or
Failed: window is not defined
I've set up a test to test this issue. It runs off a random web page that I was looking at it. It selects the footer using protractor and then attempts it using querySelector. If I enter the querySelector portion in the console, it runs the code correctly. I've also tried variations of querySelector, using window.document; this also works in the browser but not in protractor.
describe("Test", function()
{
it('This is a test to test protractor' , function()
{
browser.waitForAngularEnabled(false);
browser.get("https://facebook.github.io/jest/");
$("#footer").getAttribute("innerHTML").then( function(value)
{
console.log("inside value then");
console.log(value);
});
var queryse = document.querySelector("#footer").innerHTML;
// var queryse = browser.document.querySelector("#footer").innerHTML;
// var queryse = window.document.querySelector("#footer").innerHTML;
console.log('query selector');
console.log(queryse);
});
});
The code you're running in Protractor test case doesn't really run in browser, in fact it's executed in Node.js. You should think of it as an API that will afterwards communicate with the browser through WebDriver. That means that you cannot use browser specific JavaScript API in the code. The $ helper is just there to make the syntax easy and understandable without knowing anything about Selenium. That's why document and window are inaccessible for you. If you want to read more about that: https://github.com/angular/protractor/blob/master/docs/locators.md
#Nhor's answer is mostly correct in terms of the environments and why you cannot use document and window directly. However, for what it's worth, you can definitely find elements in the DOM through executeScript. The only question is, why do you need to do this?
Any locator you can use in the DOM, you can use in Protractor (though the syntax might be different). Here's an example, I used innerHTML because that's what you were trying in your case:
describe('Protractor Demo App', function() {
it('element test', function() {
browser.get('http://juliemr.github.io/protractor-demo/');
var el = browser.executeScript('return document.querySelector("h3").innerHTML');
el.then(function (text) {
console.log(text); // logs "Super Calculator"
});
});
});
Finally, it's important to note that this el is restricted to javascript functions from within that executeScript call. It is not a version of Protractor's ElementFinder, you cannot perform actions like getText() on it (though it is still a Promise, so you need to call .then()). You can do a console log on the el to see what's in that object.

Mocha/Chai - Test to click an element only if it's present, displayed or exists

I've just start looking at implementing tests with Selenium using a Mocha/Chai framework & am struggling with a login function. I want to check an item is present/displayed & to click it if it is.
Currently my test waits on the page loading & I want something like the following, which currently doesn't work:
var elementOnPage = driver.findElement(By.('elementId')).isDisplayed();
//var elementOnPage = driver.findElement(By.('elementId')).exist;
if(elementOnPage == true){
driver.findElement(By.('elementId')).click();
}
else{
driver.findElement(By.('anotherElement')).click();
}
I've also tried the commented out line above but neither seems to work. The code always seems to hit the 2nd part of the if statement before continuing. If the element I'm checking for is present then the test executes the next line but if it's not present then the test fails with a "NoSuchElementError: No such element".
I also didn't realise I could use stuff like exists(), isPresent() or isElementPresent() either.
Does anyone have any ideas or point me in the right direction? I'm quite new to this.
Cheers
The return value of isDisplayed is a promise, but you treat it as a boolean.
You need to do something like:
driver.findElement(By.id('elementId')).isDisplayed().then(function (displayed) {
if (displayed) {
driver.findElement(By.id('elementId')).click();
}
else{
driver.findElement(By.id('anotherElement')).click();
}
});
Or more concisely:
driver.findElement(By.id('elementId')).isDisplayed().then(function (displayed) {
driver.findElement(By.id(displayed ? 'elementId' : 'anotherElement')).click();
});
By the way By.('elementId') cannot work. I've assumed you meant By.id('elementId').

webdriverjs 'hasContent' method?

I am using webdriverjs for some testing and I am loving it so far. I am just running into an issue where I cant seem to run more broad tests. I am curious if there is any way to see if a page has specific content, but not have to pass it a particular css-selector?
I.e. I have a webpage with a bunch of text all over the place, and I specifically want to run a test to see if my page has the string "Hello World!" somewhere on the page. It doesnt matter where the string is, just that it is there.
If this isn't possible with webdriverjs, is there some other way to run this kind of test?
It is possible with webdriverjs. There is no hasContent method by default, but it's easy to create one on your own using the getText() method. getText() obtains the visible text of a given element and its subelements. So to get the whole text of a webpage you can just call in on the <html> element. Like this:
var selenium = require('selenium-webdriver');
var By = selenium.By;
var driver = new selenium.Builder().withCapabilities(selenium.Capabilities.chrome()).build();
function hasContent(content) {
driver.findElement(By.tagName('html')).getText().then(function(entireVisibleText) {
if (entireVisibleText.indexOf(content) > - 1) {
// text found
}
else {
// text not found
}
});
}

Debugging javascript functions

Is there a way in Firebug (or any other debugger) to see the functions that are being called as a page loads?
Edit: Breakpoint is really not what I'm looking for- I'd like to see the functions being called with the arguments that are being passed as I work on the page - something similar to the console - where I can see Http AJAX Post messages - with post values and the response.
Edit2: It looks like Profiler is something that I was looking for - but is there a way of looking at the parameters passed to the function and the return value?
You can always just print it out yourself. ( I know this may not be the answer you wanted.)
But what you can do is add a
<div id="debug"></div>
in your document.
Then add:
function log(str) {
$('#debug').append(str); // I'm using jQuery here
}
and then you can add the logs in your javascript, e.g.:
function myFunc(foo, bar, baz) {
log("myFunc called with ("+foo+", "+bar+", "+baz+")<br/>");
// your stuff
}
Tedious, but effective (IMO).
Firebug's console.log statement will dump stuff to the console for you, you just need to add console.log statements. For post requests and responses, use the net panel. Personally, I think adding a debug function and div to your page is overkill.
I think you need to make this more specific if you want to get more specific answers than "just use a breakpoint". Do you know what "code profiling" is? Is that what you want to do? You can google for "firebug profiler", and there is also some information right here on SO, e.g. Understanding Firebug profiler output

Categories

Resources