My requirement is I want to select the particular name from the auto-suggestion in the autocomplete text box.
So here I only know how to get the name by using the mouse down to achieve this But I know it's not a good solution to get that because we don't give the guarantee to the auto-suggestion is same as all the time in the browser.
So if anyone knows how to get the auto-suggested text names for the auto-complete text box in Selenium Web Driver using Junit (Here I am using the Junit in Selenium WebDriver to develop the automation test script).
My code:
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ENTER);
Here the above code is only working for my correct option is shows as
the second option of the auto-suggested texts.
So that's why I need how to get the text names in the auto-suggestion for the autocomplete text box.
Please the give the solutions as the JUnit Form to my question because I am using the JUnit to develop the automation test script.
Thanks
The auto-suggest box is HTML like anything else on the page. Create a locator that finds the auto-suggest panel and then parse the contents. Once you figure out the structure, you can get the text that you need and then assert that it is correct.
I can't see the full HTML from your screenshot but you can see that the list is contained in an HTML UL. The UL is the container and each LI is a list item in the dropdown. You can use that info to do something like
ul.autoCompleteGroup > li
to get all the list items. I can't see what's inside of there but at some point you should be able to use .getText() to get the auto suggest items. From there you just compare what you pulled off the list to what you are expecting with an Assert.
Please try below code
public void selectAutoCompleteItem(String itemText) {
String xpathPattern = "//div[#id='phSearchInput_autoCompleteBoxId']" +
"//ul/li/a[.='%s')]";
String xpathExp = String.format(xpathPattern, itemText);
driver.findElement(By.xpath(xpathExp)).click();
}
In that case you can use the findelements functionality. So you can say:
List<WebElement> elements = driver.findElements(by.xpath("//a[#class='autoCompleteRowLink']");
And then for
each list item you can use getText to get the exact text. Then you can assert it with the expected values
I want to select an item in a CSS dropdown but there seems to be no way to do it, we can take as an example the google hotel review page here how can I select by most recent reviews programmatically through selenium ?
Basically I want to see all the reviews of the hotel sorted by Most recent instead of most useful, but since by default are ordered by most useful I need to switch through the dropdown programmatically.
I've tried this way :
Select select = new Select(driver.findElement(By.xpath("//*[#id=\"gsr\"]/g-lightbox/div[2]/div[3]/div/div/div/div[1]/div[3]/div[2]/g-dropdown-menu/g-popup/div[2]/g-menu")));
select.deselectByIndex(1);
but I'm getting an exception(org.openqa.selenium.support.ui.UnexpectedTagNameException) saying :
Element should have been "select" but was "g-dropdown-menu"
Is there a way to simulate a click on a CSS dropdown element like this with Selenium web driver ?
Analysis:
Selenium Java API: Select.class only suitable for dropdown which use HTML select tag. For dropdown implment by other way, like JQuery dropdown plugin, Select class not support, for such dropdown you need to click on the drop down to make the options to display out, then choose the option you wanted.
Solution:
public void selectSortby(String sortBy) {
// click on dropdown to expand options
driver.findElement(By.xpath("//div[span[text()='Sort by:']]//g-dropdown-button").click();
// choose option
driver.findElement(By.xpath("//g-menu-item/div[text()='"+sortBy+"']")).click();
}
I'm trying to automate a drop down box in home page of HDFC bank in which by default 'Net banking' is selected. It does not have a Select tag and it has div tag. Also I tried to click on the drop down first and select the values one by one, but I'm getting error like
Element can't be clicked
Please help me with this.
driver.findElement(By.className("selectedvalue")).click(); //Error for this statement
try below
driver.findElement(By.cssSelector("div.loginwrap > div.selectWrapper > div.selectedvalue")).click();
driver.findElement(By.id("prepaidcard")).click();
I generated this from Selenium IDE for firefox. It is a plugin for Firefox which you can record user actions and export the test case to Java ( among many other languages).
This is the link Selenium IDE
You can also try for :
driver.findElement(By.xpath("//div[contains(text(),'NetBanking')]")).click();
driver.findElement(By.xpath("//li[#id='prepaidcard']")).click();
I am trying to select text which is already present on the browser.
I want to select that particular text and perform right click operation on it.
However, the page on the browser has disabled right click.
How can I select text in such situation?
Using a normal web browser without Selenium the only workaround that I can think about is to disable javascript to stop the script that prevents you from right clicking. So it should also work with a browser controlled by Selenium Webdriver.
I don't know if it will be OK for you because your website may be relying on javascript for essential features.
However if you don't need Javascript for your Selenium test you can try the following when you launch your driver :
FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
I assume that you already know how to perform a right click because your question was only about dealing with the problem preventing you from doing this right click. But if not, you can also refer to this answer :
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
Edit:
I'm sorry I really thought you could use Selenium actions to select the text you want but after some tests I didn't manage to perform a click and drag to select a text. The only thing that works for me in Chrome or Firefox is the following. It looks for a <p>which contains some text and then perform a double click to select a word.
driver.get("http://en.wikipedia.org/wiki/Java_(programming_language)");
WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Java is')]"));
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
However it only highlighs one word in the html element that contains your text, so it's not really convenient.
I've also tried to do Ctrl+F and type the text so that the web browser automatically select it but the browser doesn't do anything when executing :
Actions search = new Actions(driver);
search .sendKeys(Keys.chord(Keys.CONTROL,"+f")).sendKeys("Java is").build().perform();
It seems that Selenium can only send keys events to the html elements and not to the browser (in the case of ctrl+F).
I don't really see a solution for now, let's see if someone else can find a workaround. It's an interesting issue, it would also be useful for me to select a text the way you described
Move to middle of the element
Actions builder = new Actions(webDriverObject);
builder.moveToElement(element).build().perform();
Move to starting of element, click and hold, move to end
Integer width = element.getSize().getWidth();
Actions newBuilder = new Actions(webDriverObject);
newBuilder.moveByOffset(width/2,0).clickAndHold.moveByOffset(width,0).release().build().perform();
I'm currently trying to automate test cases where I need to enter values for a required Text Area field.
The text area uses TinyMCE 3.4.9
I found a blog online that suggested
selectFrame (iFrame containing tinymce)
focus (tinymce)
type (tinymce, text)
that didn't help since Selenium RC can't locate the iframe. However, I tried this with the Firefox plugin and at the very least I can select the iframe and focus the editor, but I can't enter any text. With RC, nothing I do seems to work
I also tried entering text using the html editor. So selenium can emulate clicking the button to open the html editor, then RC would either fail to find the text area or I'll get an error such that the element is no longer attached to the DOM (something along that line)
Sorry if this sounds confusing.
This worked for me:
command: runScript
target: tinyMCE.activeEditor.setContent('Replace with your text')
Got it from http://www.tinymce.com/forum/viewtopic.php?id=27960
If you have multiple tinyMCE forms on one page, I recommend
Command: runScript
Target: tinyMCE.get('textarea_id').setContent('Your Text')
Firstly you switch to frame
// driver is type of IWebDriver
driver.switchTo().frame(frameName or frameIndex);
Then you get element by id and fill it with your text
var el = driver.findElement(By.id("tinymce"));
el.sendKeys("YOUR TEXT");
WebElement bodyIframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(bodyIframe); WebElement mce =
driver.findElement(By.id("tinymce")); mce.sendKeys("This is for testing!!!!");
This worked perfectly for me
The only way I am able to get Selenium testing to work with TinyMCE is to not use TinyMCE for the test.
Use some server-side wizardry to selectively disable the TinyMCE plugin under test conditions.
This will not help you with testing the use of TinyMCE itself, but you will at least be able to fill in the form to completion and move your tests forward... :)
For TinyMCE 3.4.x and Selenium IDE in Firefox, this is working for me. Substitute the id of your TinyMCE instance for NAME below:
<tr>
<td>type</td>
<td>dom=document.getElementById('NAME_ifr').contentDocument.body</td>
<td>Editor content</td>
</tr>
This is based on https://gist.github.com/809728
We use the process described at techiebyday.blogspot.com, and it works great in current versions of Selenium RC (at least up to 2.20) and TinyMCE (post-3.4). In C#, it looks like this:
string editorFrameId = editorId + "_ifr";
selenium.Focus(String.Format("dom=document.getElementById('{0}').contentWindow.document.body", editorFrameId));
selenium.GetEval(String.Format("selenium.browserbot.getCurrentWindow().document.getElementById('{0}').contentWindow.document.body.innerHTML = '{1}';", editorFrameId, fillString));
I've been trying to use this solution:
<tr>
<td>type</td>
<td>dom=document.getElementById('NAME_ifr').contentDocument.body</td>
<td>Editor content</td>
</tr>
Replaced 'name_ifr'. First to frame name and inserted before entering into the tinymce frame and then replaced name with 'tinymce' and inserted after entering the frame where accoring to all the rules typing must happen. But all I get is errors. Somehow, I never make DOM or js commands work in my script. Is there something I am missing?
If you have one TinyMCE element in page:
var obj = ((IJavaScriptExecutor)webDriver).ExecuteScript("tinyMCE.activeEditor.setContent('" + text + "');");
I was having problems entering text in a tinyMCE editor too:
FireFox 20.01
tinyMCE 3.4.9
Selenium IDE 1.10.0
I have multiple tinyMCE editors in my form.
Solution (using Selenium IDE, which can be ported to Selenium RC easily):
Using firebug, find out the id of the tinymce iframe that replaces the textarea, e.g.
form_editor1_ifr
Strip the "_ifr" from this id to get the id of the original textarea
Use this id in the target for the runScript command in Selenium IDE:
tinyMCE.get('form_editor1_ifr').setContent('the content');
In Selenium webdriver PHPUnit_Extensions_Selenium2TestCase, this works when you use:
$this-> execute(array('script' => "tinymce.get('cms_cpg_content').setContent('Lorem ipsum dolor sit amet,...')", 'args' => array()));
When you want to add text.
I was able to solve this problem using Selenium RC, have done following steps(attached screenshot for your referral)
on tinyMCE editor, click on Advanced option icon
Click on "<>" source code icon
Source code popup appears, enter text and click Ok
text appears in TinyMCE editor
Just for your reference I have given steps with CSS locator
click on advaced option button: css= tr[id*='tinymce'] td * div[aria-label='Advanced Options'] button
click on source code: css= tr[id*='tinymce'] td * div[aria-label='Source code'] button
click on popup textarea css= div[class*='mce-floatpanel'][aria-label='Source code'] * textarea
click on ok button on popup css= div[class*='mce-floatpanel'][aria-label='Source code'] * div>button:contains('Ok')
verify text present in tinymce css=body p:contains(this is testing)
Hope this will solve your problem :)
http://odino.org/typing-into-tinymce-with-selenium/ ...looks like TinyMCE and selenium IDE hook ups don't work on 3.4.5, so my presumption is that this issue has been carried over into 3.4.9.