jQuery.Deferred exception: The string did not match the expected pattern - safari

I've a little headache with this console error, ONLY on Safari (working on MacBook actually).
I have this function:
function exists(element){
var exists = document.querySelector(element);
return exists;
}
invoked inside another function:
function includeHoverStylesheet(){
var path = $("body").attr("data-hover");
if (!("ontouchstart" in document.documentElement || exists("link[href='" + path + "'"))) {
var link = document.createElement("link");
link.rel = "stylesheet", link.href = path, document.head.appendChild(link) && document.body.removeAttribute("data-hover");
}
}
Now, on Chrome works like a charm, but on Safari the console throw this error:
1) Invalid CSS property declaration at: *
2) jQuery.Deferred exception: The string did not match the expected pattern.
3) SyntaxError (DOM Exception 12): The string did not match the expected pattern.
Someone have idea what hell happening???
Thanks in advance guys!

There is a closing bracket missing, so you are using an invalid selector. (as Roamer-1888 mentioned as comment)
Having a look at the following discussion where invalid selectors in different browsers has been tested, only Safari is that strict bringing up errors:
https://www.reddit.com/r/firefox/comments/5nbmqi/on_handling_invalid_selector_strings/
For all jquery-Users: check your jquery version, as there has been bugfixes on selector issues.
I had the same error only on Safari on this statement
var div = $('<div class="abc">');
A statement working fine on Firefox and Chrome, but not on Safari when using version jquery 1.11.1; but working fine in all of them with jquery 1.12.4.
In my case I solved it using the following syntax:
var div = $('<div>', {"class":"abc"});

Related

Element not found for Input elements on Selenide + Appium

I'm writing Android Web automated test using Kotlin+Selenide+Appium. There's already working Desktop Web version of those tests on Kotlin+Selenide.
Koltin 1.2.31
Selenide:4.11.1
Appium:java-client:5.0.4
Appium: 1.7.2
Test starts, appium server starts, browser on my device starts, page opens, element is located but it cannot setValue for it. Test works well except for Input elements and manipulations on it.
In test, I clear the field at first, then I set value for it. It actually find the element, clears it and then throws the error on that step (clear the field). So it clears the field but it also cannot found it???
Errors that appear:
Element not found {.project-scope-main-header-content-input > div > input}
Expected: exist
Caused by: WebDriverException: unknown error: call function result missing 'value'
I tried to run the tests with and without these 2 capabilities:
capa.setCapability("unicodeKeyboard", true)
capa.setCapability("resetKeyboard", true)
Appreciate any help. Thanks.
EDIT: The problem is in outdated ChromeDriver which I cannot update for some reasons.
EDIT#2:
Here's how I initialize it:
`
lateinit var driver: AppiumDriver<SelenideElement>
private val appiumServer = AppiumRunAndStop()
#BeforeClass
#Parameters("platform")
fun setUp(platform : String) {
appiumServer.restartServer()
when (platform) {
"Android" -> {
val capa = DesiredCapabilities()
capa.setCapability("automationName", "Appium")
//capa.setCapability("newCommandTimeout", 150)
capa.setCapability("platformName", "Android")
capa.setCapability("platformVersion", "8.1.0")
capa.setCapability("deviceName", "Nexus 6P")
capa.setCapability("browserName", "Chrome")
capa.setCapability("unicodeKeyboard", true)
capa.setCapability("resetKeyboard", true)
capa.setCapability("chromedriverExecutable", "pathh\\chromedriver_win32\\chromedriver.exe")
driver = AppiumDriver(URL("http://127.0.0.1:4723/wd/hub"), capa)
sleep(2000)
WebDriverRunner.setWebDriver(driver)
}
"iOS" -> {
//TO DO
}
else -> println("Platform is not correct")
}
Configuration.baseUrl = "my_url"
}`
It works with this capa.setCapability("chromedriverExecutable", "pathh\\chromedriver_win32\\chromedriver.exe") but I want it to autoupdate the ChromeDriver automatially.
Did you tried using element.sendKeys("your value") method to enter values and element.clear() to clear the text from the element?
This approach works for me. Also avoid using beta version of Appium Java client and always use a stable version which is 5.0.4.
I found the problem. My test uses outdated ChromeDriver (chromedriver=2.33.506120).
I don't know how to update it for this test.
WebDriverManager.chromedriver().version("2.37").setup() doesn't help.
System.setProperty("webdriver.chrome.driver","path\\chromedriver_win32\\chromedriver.exe") either doesn't help.
I also tried to do this:
val options = ChromeOptions()
options.addArguments("androidPackage", "com.android.chrome")
capa.setCapability(ChromeOptions.CAPABILITY, options)`
EDIT: Worked with capa.setCapability("chromedriverExecutable", "PATH") but why Selenide doesn't update ChromeDriver by itself?

How to get the webpage title using JavaScript executor

I am new bee of the automation testing. On my code, I am using the JavaScript on my Selenium automation test script (on using the JUnit). I am trying to find the current window title:
driver = new ChromeDriver();
js = (JavaScriptExecutor)driver;
driver.get(//passed url);
//after the log in my Org. I execute the command below
//to get the title of the current window I execute the below command
js.executeScript("document.getElementsByTagName('title').innerHTML;");
But the above command will return the answer as the null instant of
getting the title of the window. So does anyone know what's wrong with
my command?
But I get the title of the window on executing on the console log of the page. So I don't know what's wrong with my code.
Thanks.
Mohan Raj S.
Change the following line:
document.getElementsByTagName('title').innerHTML
to:
return document.getElementsByTagName('title')[0].innerHTML
in code it will be:
js.executeScript("return document.getElementsByTagName('title')[0].innerHTML;");
getElementsByTagName returns array of elements. So we need to pass the index value.
Web driver interface provides get title method. It is very simple.
String title=driver.getTitle();
System.out.println("Title is" + title);
You could do it like this also
function titleCheck() {
promise = driver.getTitle();
promise.then(function(title) {
console.log("The title is: " + title);
});
}
reference: https://blog.testproject.io/2018/03/08/selenium-javascript-best-practices/
though their var test- stuff doesn't work for me I had to remove it.
JavascriptExecutor js = (JavascriptExecutor)driver;
String text = js.executeScript("return document.title;").toString();

Element present fails due to iframe issue for getting image src using Selenium WebDriver

As we know iframe can be counted using frameslist but that doesn't work for me and gives blank output although frame count gives me count as 2. I'm using Selenium WebDriver and Java.
Basically I want to get img source's data-mce-src starts with cid and dfsrc ends with # according to below screenshot.
I tried :
public static final String imageAttachment="css=img[data-mce-src^='cid']&&[data-mce-src$='#']";
which works fine using sIsElementPresent in selenium 1.0 but it fails in webdriver using findElement. In fact it doesn't identify iframe itself.
Expected:
css=img[data-mce-src^='cid']&&[data-mce-src$='#'] element present?
Code:
WebElement we = null;
List <WebElement> framesList = webDriver().findElements(By.tagName("iframe"));
for (WebElement frame:framesList){
System.out.println(frame.getText()); // returns nothing
}
int listSize = framesList.size();
webDriver().findElement(By.xpath("//iframe"));
System.out.println(listSize);
Also tried:
webDriver().switchTo().frame(webDriver().findElements(By.tagName("iframe"));
we = webDriver().findElement(By.cssSelector("html body div img"));
System.out.println(we.getAttribute("src")); // returns nothing
You should try as below :-
webDriver().switchTo().frame("Editor1_body_ifr");
we = webDriver().findElement(By.cssSelector("body#tinymce img"));
System.out.println(we.getAttribute("src"));
try {
webDriver().switchTo().frame("Editor1_body_ifr");
we = webDriver().findElement(By.cssSelector("html body img"));
System.out.println(we.getAttribute("src"));
System.out.println(we.getAttribute("data-mce-src"));
System.out.println(we.getAttribute("dfsrc"));
} finally {
webDriver.switchTo().defaultContent();
}

nightwatch selenium command "elements" returns error about first parameter, even when I pass in a css selector or xpath expression

I'm using nightwatch with selenium for automation testing. I'm trying to use the selenium command 'elements' which takes a css selector or xpath as the first parameter, but keep getting the following error :
Error while running elements command: Please provide any of the following using strings as the first parameter: class name, css selector, id, name, link text, partial link text, tag name or xpath
My use is like this:
module.exports = {
"My test" : function (browser) {
...
// want to get all the input elements from the document
browser.elements('input','name', function(els){
// of xpath like this
browser.elements('//input','name', function(els){
});
}
}
Any ideas why I'm getting this error? Thanks!
Ah I figured it out - the first param is a keyword which has to be one of these (from nightwatch selenium protocols.js)
var check = /class name|css selector|id|name|link text|partial link text|tag name|xpath/gi;
So the command needs to look like this :
browser.elements('tag name', 'input', function(el){
})

Jquery .get not working in Firefox and IE

I'm using the awesome GoSquared API to get the number of current visitors on my Site.
I have build a Jquery Script, that automatically updates the number every two seconds with Jquery .get, but this doesn't seem to work in IE and Firefox.
JSFiddle
Thanks :)
In Firefox data is a string for some reason. You can specify the data type of the response explicitly:
$.get('url', function(){}, "json");
Otherwise you can turn it into an object like this:
if (typeof data === "string"){
data = JSON.parse(data);
}