How to find the highlighted text using selenium web driver in Java? - java

Hello I have tried the following link.
It is working only if we select the text manually.
But my case is like, we have found the text using the text search.
Is it possible to find the selected text using selenium web driver??

We can do this by using getCssValue method available in WebElement.
Demo:
WebElement branch = driver.findElement(By.xpath(".//h3[contains(text(),'locator')]"));
String background = branch.getCssValue("background");
Assert.assertEquals(background, "We know the actual bg color");

Related

How to check the text on an overlay using Selenium?

I'm clicking in on a button on a webpage using Selenium. The button creates a file which can be downloaded now. For this, a overlay is shown in Internet Explorer (yes, I HAVE to use this browser, it's a requirement).
Now I have to check the text on the overlay ("öffnen oder speichern" see my screenshot). I can imagine that it there is a solution using JavaScriptExecutor but I simply couldn't found a solution.
I also tried to find it in innerHTML-without success.
It's not an alert so I can't use Driver.switchTo().alert();
My Code still doesn't contain more than clicking on a button using XPath.
Actions action = new Actions(driver);
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
String exportButtonXPath = generalHelper.getProperty("buttonCSVExportXPath");
WebElement exportButton = driver.findElement(By.xpath(exportButtonXPath));
action.click(exportButton).perform();
Do you have a solution how can test the text on this popup?
Actually, it is not related to the web browser any more. You need to interact with it as a desktop window.
->If you want to click it using selenium, you can locate its coordinates and use click by coordinates using selenium.
->If you want to accept to download it, you can find a capability to accept downloading by default (except IE).
->If you want to check the text value, for sure you've to automate it as desktop not as a web.

How to get the text from the auto suggestion of the auto complete text box?

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='autoCompleteRowLin‌​k']");
And then for
each list item you can use getText to get the exact text. Then you can assert it with the expected values

How to find the HTML element id of text field pop up window?

I am trying to automate browser using Selenium in Java. However I am unable to find the element ID of a text field in pop up window to call it in Selenium Java.
The text field whos HTML element ID that I am trying to find is Project (as seen in attached photo)
Selenium script:
driver.findElement(By.id("project-name")).sendKeys("TEST: Automatation by Selenium");
Any suggestions?
This works for me:
driver.findElement(By.id("project-field")).sendKeys("TEST: Automatation by Selenium");
Are you sure that the id is project-name and not project-field? In my Jira template project-field is used as id.

How would I enter text into a website with java?

I'm just trying to find out how to tell java to open a website and enter text (preferably a string value) into a text field
For example go to Google and search any text( it does not have to be user entered)
I realize that it wont actually open any browser or print anything from a website. I just need to know this basic part to build on for my program.
If you try to query to Google in your example and want to get the search result, you can use query string and read its html result
Figured it out . I had to use a combination of Selenium and HtmlUnit. My code is something like this
WebDriver driver = new HtmlUnitDriver();
driver.get("https://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("Hello");
driver.quit();

How to enter text into tinymce text area using Selenium RC for Eclipse Java

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.

Categories

Resources