Selenium sendText - testing

I need to preface this with "I am a noob".
In WatiN I was able to use sendText("text"); which would send the whole text rather than typing it out one character at a time which is what sendKeys() does. I have looked quite a bit for a sendText() option in Selenium and cannot seem to find anything which works.
Is there a sendText() option for selenium, if so could you provide a code example?

In Selenium RC (the older JavaScript-fueled Selenium that is no longer actively developed), there is the type() method.
In WebDriver (also known as Selenium 2), there's no such thing. However, you can easily emulate it via JavaScript:
// only if your driver supports JavaScript
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement elem = driver.findElement(By.whatever("something"));
js.executeScript("arguments[0].value = 'some text'", elem);

Related

How to resolve org.openqa.selenium.WebDriverException?

I am writing an automated test and want to report bugs, if occur, directly in the repo at GitHub. The step which fails in my program is the Submit new issue button from GitHub Issue Tracker.
Here is the code:
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
And the exception:
org.openqa.selenium.WebDriverException: Element is not clickable at
point (883, 547.7999877929688). Other element would receive the click:
div class="modal-backdrop"></div
The following command also does not work:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", sendIssue);
How can I make it clickable? Is there any other way by which I can resolve this issue?
This is happening because when selenium is trying to click ,the desired element is not clickable.
You have to make sure that the Xpath provided by you is absolutely right.If you are sure about the Xpath then try the following
replace
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
with
WebElement sendIssue =(WebElement)new WebDriverWait(DRIVER,10).until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button")));
sendIssue.click();
If that doesn't work ,You will get an Timeout exception, In that case try incaresing the timeout amount from 10 to 20.
If it still doesn't work please post a screenshot of the HTML.
You need to write something in the issue title and description to make the issue clickable are you sure you are not making that mistake of clicking the button without writing anything in those places I am adding screenshot for your convenience.
Selenium Webdriver introduced in a previous version (v2.48) a new behavior that prevent clicks on elements that may be overlapped for something else (a fixed header or footer - for example) or may not be at your viewport (visible area of the webpage within the browser window).
You can see the debate here.
To solve this you will need to scroll (up or down) to the element you're trying to click.
One approach would be something like this post:
Page scroll up or down in Selenium WebDriver (Selenium 2) using java
Another, and maybe more reasonable, way to create a issue on Github, would be using their API. Maybe it would be good to check out!
Github API - Issues
Gook luck.
This worked for me. Instead of HTML browser this would be useful if we perform intended Web Browser
// Init chromedriver
String chromeDriverPath = "/Path/To/Chromedriver" ;
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);

Access new window elements through selenium RC

I am new to Selenium. This is my first attempt. And I want to access the Elements in the new window through Selenium RC. I have a page with a hyper link clicking on it will open new page. I want to enter username and password elements in the new window. Html Code is
Employee Login
and the new page elements are "emailAddress" and "password" for login.
My selenium code is
public static void main(String args[]) throws Exception
{
RemoteControlConfiguration rc=new RemoteControlConfiguration();
rc.setPort(2343);
SeleniumServer se= new SeleniumServer(rc);
Selenium sel=new DefaultSelenium("localhost",2343,"*firefox","http://neo.local/");
se.start();
sel.start();
sel.open("/");
sel.windowMaximize();
//sel.wait(1000);
sel.click("empLogin");
//sel.wait(2000);
//sel.openWindow("http://myneo.neo.local/user/login", "NewNeo");
//sel.waitForPopUp("NewNeo", "1000");
//sel.selectWindow("id=NewNeo");
Thread.sleep(20000);
sel.type("emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("password", "xxxxxxxx");
}
First I tried with normal approach, where it failed to identify the elements. Then I tried with open window and selectWindow options, where it through errors like
"Exception in thread "main" com.thoughtworks.selenium.SeleniumException: ERROR: Window locator not recognized: id
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:109)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:103)
at com.thoughtworks.selenium.DefaultSelenium.selectWindow(DefaultSelenium.java:377)
at Demo.main(Demo.java:24)"
Some one told me that it is not possible with Selenium RC. It can achieved through Selenium Webdriver only. Is it true?
Please Help,
Thanks in Advance.
Selenium RC is not maintained anymore, I would recommend you to use WebDriver instead. Selenium RC is a Javascript application where as WebDriver uses browsers Native API. Therefore browser interactions using WebDriver are close to what the real user does. Also WebDriver's API is more intuitive and easy to use in my opinion. I don't see HTML in your question, but you could do start with something like this,
WebDriver driver = new FirefoxDriver();
WebElement login = driver.findElement(By.id("empLogin"));
login.click();
I will say, do as #nilesh stated. Use Selenium WebDriver.
Answer to your specific problem though:
You are failing at this line because you are not specifying a selector strategy.
sel.click("empLogin");
If the id attribute is empLogin then do
sel.click("id=empLogin");
There are other selector strategies you can use:
css=
id=
xpath=
link=
etc...
You can see the full list here.
You will also fail here due to the same issue:
sel.type("emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("password", "xxxxxxxx");
Put a selector strategy prefix before those fields.
sel.type("name=emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("name=password", "xxxxxxxx");

Selenium clicking a button

I am trying out selenium for the 1st time and I have a quick question. When I call the click() method on a WebElement, I noticed that it is a void type method. So does the HtmlUnitDriver hold the updated page that is rendered after the click() happens?
Yes. The WebDriver interface is controlling the browser, however it's still the browser (in your case, HtmlUnit) that does most of the work and remembers the state of teh page etc.
Therefore, WebDriver as such doesn't really have a state (an overly simplified statement, but true for your purpose). When you send a click() command, it performs it in the browser, than waits for the browser to complete its job (load the new page), then again waits for your commands on the new page.
WebDriver always operates on what the browser currently has.
I can see from your question that you are using HtmlUnitDriver. JavaScript is disabled in this driver by default (for explanation click here). This driver uses the Rhino JavaScript engine and is not used in any of the popular browsers. This might explain why the actions you are trying work fine in Firefox but not in Selenium.
You could try enabling JavaScript in HtmlUnit as follows:
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
But instead I would recommend using FirefoxDriver.
WebDriver driver = new FirefoxDriver();
This should emulate the behavior you're seeing when you navigate through the webpages yourself.

Selenium WebDriver Issue with cssSelector

I am trying to click on a button with using a CSS selector in Selenium 2.0 WebDriver. The issue is the script which I am able to run with Selenium RC is not working with WebDriver.
Code:
Selenium RC:
selenium.click("css=.gwt-Button:contains('Run Query')");
which works absolutely fine.
Selenium WebDriver:
driver.findElement(By.cssSelector(".gwt-Button:contains('Run Query')")).click();
which does not work.
I am using: selenium-server-standalone-2.9.0.jar with Firefox version 5.0. Could anyone help me in figuring out why the cssSelector is not working with WebDriver?
'Contains' is being deprecated from CSS 3. Webdriver supports whatever natively supported by the browser. It works in selenium RC because RC uses Sizzle library for css selectors and it supports 'contains'. Did you try something like,
WebElement element = driver.findElement(By.cssSelector(".gwt-Button[type='button']");
element.click();
If this is not unique then perhaps you might need to filter it further down. If your site uses jQuery then you could use 'contains' selector from jQuery.
JavascriptExecutor js = ((JavascriptExecutor)driver);
WebElement element = (WebElement) js.executeScript("return $(\".gwt-Button:contains('Run Query')\")[0];");
element.click();
that syntax looks valid and when I dropped some elements on a test page and then went to my console and did $(".gwt-Button:contains('Run Query')").length I got a 1 when I changed the text of the buttons. So the only thing I can think of is maybe you have a Run Query button that exists on the page but isn't displayed and so you get the runtime exception. You might want to do a check to see if it's displayed before clicking on it. Or creating a list of the elements and then removing all hidden (display == false) elements.

Selenium 2.0 / WebDriver clickAt() method unsupported

Selenium clickAt() function is throwing "Unsupported" exception while using with WebDriver (WebDriverBackedSelenium or just Selenium 2.x using ChromeDriver).
Is there any way to use this Selenium function via WebDriver?
Adding some code for context ...
ChromeDriver driver = new ChromeDriver();
driver.findElement(By.id("someID")).clickAt("25, 25");
.clickAt() method isn't even recognized ... however, using the WebDriverBackedSelenium is what provides the Unhandled exception.
You have to use Advanced User Interactions API
Click at the specific point inside an element looks like following:
ActionChainsGenerator builder = ((HasInputDevices) driver).actionsBuilder();
Action action = builder
.moveToElement(elementLocator, xOffset, yOffset)
.click()
.build();
action.perform();
At the moment, it is implemented for HtmlUnitDriver and InternetExplorerDriver only, other drivers are work in progress.
I have sometimes had similar problem and have fired the two MouseDownAt & MouseUpAt to solve the issue.. Seems as some JavaScript don't fire ok with clickAt always
Before you use click command on locator. you should use mouseOver on it.
Normally. this problem happen when link that need to click hidden or invisable.