Matching elements by property ends with in WebDriverIO - selenium-webdriver

I am used to Selenium WebDriver were I can do something like this:
ReadOnlyCollection<IWebElement> magicPills = _webDriver.FindElements(By.CssSelector("span[id$='_Blue_Pills']"));
How do I do the same thing in WebDriverIO? I couldn't find anything in the docs that stated StartsWith, EndsWith, or whatever.
My first failed attempt is:
const magicPills = $('span.$_Blue_Pills');

Give a try as like below in wdio:
const magicPills = $$('span[id$='_Blue_Pills']');
$() returns a webElement not elements
and you can use the same cssSelector you tried in selenium_webdriver(because wdio will automatically resolves to cssSelector internally).
Please try the above and see if it works.

Related

I'm having trouble locating a linkText with <em> tag in selenium with java

the HTML looks like
<span class="linktext"><em>M</em>asters</span>
xpath - //*[#id="mastersNavButton"]/span
I tried with below codes but didn't work
driver.findElement(By.xpath("//*[#id="mastersNavButton"]/span"));
driver.findElement(By.partialLinkText("asters")).click();
First check this:
List<WebElement> emElements = driver.findElements(By.tagName("em"));
System.out.println(emElements.size());
If you get 0, you'll need to wait until the page is loaded and elements reachable by selenium and/or switch to frame.
See this https://www.guru99.com/implicit-explicit-waits-selenium.html
and this https://www.guru99.com/handling-iframes-selenium.html
Please use the following code:
driver.find_element_by_xpath("//span[contains(#class,'linktext')]//em[contains(text(),'M')]").click()

Jasmine - How to Write Test for Array with Named properties and Object

I ran into a strange situation today, thanks to Javascript. I have a Object that look something like this.
$scope.main = [{main : 1},service:true];
Now when I try to expect this inside the jasmine test case for equating the Objects :
expect($scope.main).toEqual([{main : 1},service:true]);
This gives me an error :
Unexpected Token.
Strangely, This is a valid object for Javascript. But Jasmine is not able to accept that.
Is there any way to test this?
Thanks in advance!
EDIT : Attaching a structure screenshot.
Update
I see now based on your screenshot that you are creating the main object in multiple steps. I've shortened it to the following:
var main = [{main: 1}];
main.service = true;
In dev-tools, you are seeing main as something that looks like this: [{main: 1}, service: true].
However, don't be mislead. Dev-tools is showing you a structure that is just meant to be informative. You can't actually create that structure in one line of javascript, because it is invalid. You have to create it in multiple steps, like you have.
This is why when you try to create it in your test in one line, you are getting an Unexpected Token. error. In your test, you have to create the expected object in a similar fashion to how you created your main object. For example:
var expected = [{main: 1}];
expected.service = true;
expect(main).toEqual(expected);

Unable to locate "Login" button in Selenium

My code looks like this:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//driver.findElement(By.xpath(".//button[starts-with(#type,’submit’)]")).click();
driver.findElement(By.xpath(".//*[/html/body/div[4]/div/form/div[3]/div[2]/button']")).click();
The error is:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string './/*[/html/body/div[4]/div/form/div[3]/div[2]/button']' is not a valid XPath expression.enter image description here
Please let me know, how can I automate this?
First of all I would suggest you to go for Relative based Xpath than Absolute based.
use this below:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElementByXpath("//button[starts-with(#type,'submit')]")).click();
You have kept . before '//' which is not required and if this doesn't work lemme know.
driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/button")).click();
[] is for conditions. Example:
driver.findElement(By.xpath("/html/body/table/tbody/tr[contains(., 'bla')]"));
./ or .// is for finding children elements of an element. Example:
var RcdRow = driver.findElement(By.xpath("/html/body//tr"));
var RcdTd = RcdRow.findElement(By.XPath(".//td[3]"));
There is an extra quote in your code after "button". Also .//* is used for relative path so you don't have to use it here since you are using absolute path to the button element.

Code optimization in selenium webdriver for driver.findElement

The code which i have written uses find Element method more than 32 times:
i wanted to create a common method for find Element
should i declare any generic method ?
A bit more of info on your code will help answer this. If you are trying to access different elements on your page then you would directly or indirectly end up making these 32 calls.
So first check if you need 32 different elements or not. If not, consider storing the results in variables and reusing them (again depends on your code/flows).
Although it won't make any difference in doing it, but if still you want you can make a method somewhat like this:
public WebElement find(String type,String locator){
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement we;
if(type.equalsIgnoreCase("xpath")){
we = driver.findElement(By.xpath(locator));
}
if(type.equalsIgnoreCase("id")){
we = driver.findElement(By.id(locator));
}
// and so on...
}
And you can use it like this:
WebElement newButton1 = find("id","button1");
WebElement newLink1 = find("xpath","//a[text()='xyz']");

sendKeys(Keys.TAB) not working in JMeter Webdriver Sampler

I am trying to enter a value into a textfield then Tab to the next field (which also enters the value). The Keys.TAB method does not seem to be working.
My code is as follows:
var Keys = JavaImporter(org.openqa.selenium.Keys)
var input = WDS.browser.findElement(pkg.By.xpath('xpath_to_input'))
input.sendKeys('value')
input.sendKeys(Keys.TAB)
I am getting the following error:
sun.org.mozilla.javascript.internal.EvaluatorException: Can't find method org.openqa.selenium.remote.RemoteWebElement.sendKeys(string). <Unknown source>
Thank you for your help. I have tried all sorts of things and it will not work.
In addition to what ekuusela suggests there are 2 more options:
Use \t escape sequence like:
input.sendKeys('value\t');
Use java.awt.Robot approach as follows:
input.sendKeys('value')
var robot = new java.awt.Robot()
var keyEvent = java.awt.event.KeyEvent
robot.keyPress(keyEvent.VK_TAB)
robot.keyRelease(keyEvent.VK_TAB)
Remember that "Robot" approach simulates native key and mouse event on the machine where it is executed so if you use remote webdriver instance it won't play.
For more WebDriver Sampler tips and tricks see The WebDriver Sampler: Your Top 10 Questions Answered guide.
If you use Java 6 you must pass the string in an array, like this:
var input = WDS.browser.findElement(pkg.By.xpath('xpath_to_input'))
input.sendKeys(['value'])
input.sendKeys([Keys.TAB])
http://jmeter-plugins.org/wiki/WebDriverSampler/

Resources