how to create common element which can accept text & validate it - webdriver-io

I want to pass multiple text through feature file & want to validate it, so want to create one common element which can take text & validate over the page.
I have tried :
get elementText() {
return $('//\*\[contains(text(),"")\]')
}
but don't know how we can parameterize the text & pass it to element. I know this solution using Java & Selenium webdriver, but same i want to achieve it through webdriverIO.

Related

Selenium XPath for WebTable using parent & sibling

I am trying to automate the web table on the demoqa site https://demoqa.com/webtables where I am aiming to click the edit and delete button for a specific user. Please find the attached screenshot for reference.
Screenshot
I am trying to click the edit and delete button for the user 'Cierra' hence I have created the customize XPath '//div[contains(text(),'cierra#example.com')]//following::div[text()='Insurance']//following::div//div//span[#title='Edit']'
Trying to click the edit and delete button using the contains text with email 'cierra#example.com' however I see four results even I use the unique username. Could anyone help me with this?
(//div[contains(text(),'cierra#example.com')]//following::div[text()='Insurance']//following::div//div//span[#title='Edit'])[1]
you can enclose the result in bracket and call [1] , to get first one:
But you don't have to over complicate it , just get email then go back to parent and get span under that parent ,:
//div[contains(text(),'cierra#example.com')]/..//span[#title="Edit"]
if you still want to use fancy xpath locator then use :
//div[contains(text(),'cierra#example.com')]/following-sibling::div[contains(text(),'Insurance')]/following-sibling::div//span[#title='Edit']

How to add text in pop up window in selenium

I am trying to test a website using selenium(with java). On my website, there is an option to add a new class. When we click on the add new option button, a pop-up window will come and we can enter our new class name. But using selenium I cannot enter alphabets into the pop-up window. I can only enter numbers in that field. The field accept both alphabets and numbers when we enter the data manually. How can I add alphabets to the pop window in selenium?
Here I attach my code below :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"modal-add\"]")));
driver.findElement(By.xpath("//*[#id=\"class\"]")).sendKeys("LKG");
driver.findElement(By.xpath("/html/body/section/div/div[3]/div/div/form/div[2]/button[2]")).click
screenshot of the field - Class_field_screenshot
As mentioned above you are not able to add alphabets in textfield using sendkeys function. I haven't heard this type of issue before please confirm that either that field accept alphabets or not. if that feild accept alphabets then try using another way like javascript executor to send text as mentioned below
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('#id').val("sendtext")");
Can you try Clipboard class for copy and paste string value in class field ? Make sure you click first in class text field and then use paste code. As you say by keyboard you are able to enter alphabet so try to use keys class too.In both cases your keyboard is going to use so may be it helps.

How do I create a logic for href followed by text

Passed
How do I find the element based on the text followed by the href?
I have tried this
IWebElement ele = Driver.driver.FindElement(By.XPath("//a[contains(#href,'grdCoverageSelected' and text(),'Passed']"));
ele.Click();
Also, there are some other controls like
Passed,TBD,Pending,and failed
Also, need to implement a logic if I find passed then it will go to next page and if I will find any other of the tags then it will click the hyperlink and change from TBD to PAssed or failed to passed or pending to passed from the drop-down list
Please try with below solutions.
using xpath = //a[#href='href url'][contains(text(),'text')]
using css = div:contains("Click here") or div>.className:contains("Click here")

Get text associated with check box in selenium where html code does not have value or name property

I have a Radcombobox in my application which has several check boxes in the combo box. What I wish to do is to fetch the text associated with the check box. I searched on internet and came to know that text can be fetched from name or value property in HTML code, but problem is that my HTML code does not have such properties.
HTML code:
input class="rcbCheckAllItemsCheckBox" type="checkbox"
Check All
What i wish to do is to fetch value "Check All".
Using code x = driver.findElement(By.xpath("/html/body/form/div[1]/div/div/div/input")).getText();, value returned is blank.
You can get the text through JavaScript API of Rad Controls. You can check the official documentation- http://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/client-side-programming/overview
Basically, you first locate the element in JS and then use the official control method, in your case get value method. You can also perform several other useful operations if you need to.
You can execute JS in WebDriver with the following code:
IWebDriver driver;
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("return document.title");

How to manipulate user selected text using webdriver?

Lets say i have the following snippet in my web page:
<p> This is some text </p>
I want WebDriver to select "some" in this text, as if the user selected it. How should i do this? I know how to get the <p>-element:
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p"));
System.out.println(p.getText());
The println prints "This is some text".
I tried sending keys to the element, and that used to work(in selenium 2.0b), but i'm using selenium 2.6.0 now, and it stopped working:
editable.sendKeys(Keys.chord(Keys.SHIFT, Keys.LEFT));
Does anyone have ideas? I'm using the FirefoxDriver.
I did this once for Firefox using Javascript. Basically I used the range object in Firefox to select the text. Change the start and end range index based on what you want to select. This would not work in IE, because selecting range is conceptually different in IE. I don't have the IE code handy but since you are concerned about FF, you could give this a shot.
Let me know if you interested in IE text range.
String script = "var range = document.createRange();" +
"var start = document.getElementById('idofthedivthatcontainstext');" +
"var textNode = start.getElementsByTagName('p')[0].firstChild;" +
"range.setStart(textNode, 8);" +
"range.setEnd(textNode, 13);" +
"window.getSelection().addRange(range);";
((JavascriptExecutor)driver).executeScript(script);
You are trying to select the contents of the p tag by drag select right I am not sure if that is objectively possible to be done by the user as your are suggesting.. Selenium now tries to mock the exact action that a user can perform on a browser. thus you just cant send a shift and left select key on the p tag and expect it to select unlike a textbox where it is very much possible but you might probably have to click on the text box first to get it into focus.
Here is what I would suggest to achieve this, not sure if it will work.
a) send the left click on the p tag
b) hold the shift key
c) drag the mouse to the end of the p tag.
Hope that helps.
Use the .Text property of the IWebElement
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p").Text).ToString();
editable.Replace("This is ", "").Replace(" text.");
System.out.println(p.getText());