Simulate Enter key after typing URL - selenium

I need to know how to simulate the Enter key press after typing in a URL in a mobile web browser using sendKeys().I tried
driver.sendKeys("http://www.google.com\n");
which did not work and
driver.sendKeys(Keys.ENTER);
just clears the whole url box.
So it would be helpful to know.
BTW i am using Appium server for the automation.

Try this, hope this will help.
driver.sendKeys("http://www.google.com", webdriver.Keys.ENTER);

This will do it for you. Enjoy!
WebElement we = driver.findElement(By.cssSelector('the selector');
we.sendKeys("your text" + Keys.ENTER);
If you'd prefer to method chain the whole call
driver.findElement(By.cssSelector('the selector').sendKeys("your text" + Keys.ENTER);

Related

Karate Robot: Not able to click button using image

I am using Karate robot for clicking a button using image.
Below is my code:
robot { app: '^Chrome', highlight: true }
robot.input('OracleDriver')
delay(2000)
robot.click('delete.png')
Sometimes I am able to click delete button for delete.png but other times I am not.
So facing this issue intermittently.
Yes, finding by image is indeed not very reliable and should be only used as a backup when normal windows locators don't work.
I have only the following suggestions:
find a windows locator that works. note that you can navigate from a known locator using someElement.parent.firstChild etc: https://github.com/intuit/karate/tree/master/karate-robot#element-api
try to standardize the resolution that works best
see if using OCR works better
contribute code to Karate to make this better
look for another solution
I tried clicking delete button by using it's class and it is very reliable, below is my code
waitFor('.icons8-delete-blue').click()
I also followed #Peter's suggestion (someElement.parent.firstChild) and it worked for me!, below is the code
waitFor('.modal-footer').children[0].click()
Thanks #Peter for the suggestion

How to automate a textbox where area code of phone number gets added automatically?

Scenario is with an eComm website:
Cases are :
Open https://www.flipkart.com/
Click on Login URL
Top Window opens up.
Try adding phone number [Don't login using eMail ID]
Find a +91 gets added as a prefix automatically whenever you add a phone number.
I am using Selenium WebDriver and Java for automating this scenario. Any help would be appreciated.
Thanks.
Nothing special for this specific case here. Just open up the login window and use the code below:
driver.findElement(By.cssSelector("input[class='_2zrpKA']")).sendKeys("Your phone number goes here");
Somehow we are unable to get Dynamic Xpaths.
Try this absolute path
driver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input")).sendKeys("1231");
There's a bug in Firefox WebDriver and now it got fixed.
Download the latest Gecko Driver from here
Closing this. It now works for me with the configuration FF 53, geckodriver-v0.16.1-win64, Windows 64 Bit and Selenium 3.4.0
More can be checked here : https://github.com/mozilla/geckodriver/issues/659
WebElement phone = driver.findElement(By.xpath("//[#id="load_form"]/fieldset[2]/input"));
phone.sendKeys("123456789");
This worked for me!!!

Mouse hover in selenium using Java

Need perform hover on Electronics highlighted on below image
cd.get("http://flipkart.com/");
cd.manage().window().maximize();
Actions mobile = new Actions(cd);
WebElement electronics= cd.findElement(
By.xpath(".//*[#id='container']/div/header/div[2]/div/ul/li[1]/a/span"));
//electronics.click();
mobile.moveToElement(electronics);
mobile.build().perform();
This post contains the same question as yours, please consider viewing the first answer, it was accepted.
PS : please let me know if it worked, else we can search another solutions! Good luck.

I need to use a keyboard option like Control Alt s in Selenium Webdriver

I need to use a keyboard option like Control+Alt+S in Selenium Webdriver. can anybody help me out in this scenario?
If you are using JAVA:
String saveIt = Keys.chord(Keys.CONTROL, "s");
driver.findElement(By.whatever("anything")).sendKeys(saveIt);
UPDATED
Thanks to Slanec to point out that the Keys.chord can accepts more than two keys.

Is there a way to make actions optional in Selenium IDE?

This is a bit of a newbie question, but... is there a way to make actions optional in Selenium IDE? I'll provide a use case.
In the app I'm testing, users see a "hey, you're agreeing to the ToS by logging on"-type modal window at the beginning of each session. They have to click OK to continue, and they don't see the window again until the next session.
Based on what I've seen so far, I need to have one test suite for the first test each day, and a second test suite for all the others. The second suite is exactly the same except that it doesn't have the "click okay to dismiss the initial modal window" step. Alternatively, I could just remember that my first run of the test each day will fail, and that I have to run the test again.
Both of those "solutions" seem unnecessarily awkward. Can I just make the click command optional?
Create a javascript file called user-extensions.js with the below code. Then go into the Selenium IDE Options dialog and select your user-extensions.js file, restart Selenium and you'll be able to choose TryClick which will work the same as Click but suppress any errors.
Selenium.prototype.doTryClick = function(locator) {
try {
return Selenium.prototype.doClick.call(this,locator);
} catch(err) { return null; }
};
Perhaps overdue, but for future searchers.
You could use the if and endIf statements within the IDE.
If you are using cookies to decide whether to hide the ToS dialog, you could check that a certain cookie is set and if so, skip the click.
I haven't used the selenium IDE much, but I think doing the check would be much easier if you are using a programming language. I am not sure how to do it in HTML tests.
If you are using HTML, you could have a look for Selenium IDE Flow Control and see if that can do what you need. I haven't used this myself, but if looks like it supports if statements. You could use verifyCookie to check if the cookie exists.
Hope that helps.
As aj.esler pointed it out Selenium ID Flow control is a good solution that has worked for me.
Here is the Firefox add on
I use the gotoif, here is an example about how you can use it. When skip value is 1 then it will go to the label=jump line and will not execute everything from gotoif like to label=jump .
Another extremely useful flow control add-on for the IDE is SelBlocks
It will give you the ability to use: if/else/for/foreach/while and even a way to read variables from an XML file.
Use http://wiki.openqa.org/display/SEL/flowControl addon.
Make something like this :
1.storeElementPresent | //button[#name="cookie_law_accept"] | cookie_law
2.goToIf | storedVars['cookie_law']!=true | end
3.click | //button[#name="cookie_law_accept"]
4.label | end
Explain:
1.If element is present it will be stored as a "cookie_law" with value "true"
2.If cookie_law is not "true" - go to label "end" - other way go to next step
3.Click to cookie accept button (only when itsenter code herepresent because it its not - you go to "end" label and you skip this command)
4.You go here if there is no cookie law button :)