I want to check the number of characters I can insert in a text field, and was thinking of using 'for loop' but it would not help as Selenium tries to insert more than required character the field will not accept but test goes on without any failure, so is there a way to get character count of the text field?
Would this work?
final String myLongString = "Something horrrrribly looooong";
final int longStringLength = myLongString.length();
// assuming driver is a healthy WebDriver instance
WebElement elem = driver.findElement(By.id("myInput"));
elem.sendKeys(myLongString);
// it's possible that you'll first need to lose focus on elem before the next line
int realLength = elem.getValue().length();
assertEquals(longStringLength, realLength);
Using Protractor I captured the actual text in the field and then did a forloop to count each letter.
element(by.css('elementPATH')).getAttribute('value').then(function(words){
//forloop to count each word
var x = 0
for(var i = 0; i < words.length; i++) {
x = x + 1;
};
//check condition
expect(x).toBe(200);
return true;
});
Let me know if this helps.
Related
I want to click a variable value which is stored in a string and below is my code.
OrderConfirmationData orderConfirmationData= (OrderConfirmationData)Serenity.sessionVariableCalled("OrderConfirmationData");
int maxtries=0;
System.out.println("Order is available in NTI" + EfloristConstants.lastname.get());
List<WebElement> recordcount = getDriver().findElements(By.xpath("//table[contains(#id,'ctrlDWController1_DWDetails1_gvNTI')]/*/tr[not(contains(#style,'bold'))]"));
int j=16;
for(int i=1;i<=recordcount.size();i++) {
j=j*i;
String record = "(//table[contains(#id,'ctrlDWController1_DWDetails1_gvNTI')]/*/tr[not(contains(#style,'bold'))]/td)["+j+"]";
String recipientname= element(By.xpath(record)).getTextValue();
if(recipientname.equals(orderConfirmationData.getShipFirstName() != null)) {
System.out.println("Order is available ");
}
j=16;
}
want to click on the recipientname when the, if the condition matches i.e want to perform click inside the If condition.
Add the below line in if condition:
element(By.xpath(record)).click()
I have one dynamic table. in row=1 and column=3 the value of the cell is 2. I don't know any xpath, id, name etc.
To use td and tr how can I get that value.
Sample table
WebElement table= driver.findElement(By.id("table"));
/* List of all rows*/
List< WebElement> rows = table.findElement(By.tagName("tr"));
int row_count = rows.size();
/*Looping rows*/
for(int i=0;i< row_count;i++)
{
/* List all columns*/
List < WebElement> columns = rows.findElement(By.tagName("td"));
int column_count= columns.size();
for(int j=0;j< column_count ; j++)
{
String celltext= columns.get(j).getText();
System.out.println(celltext); ///This will print all values inside the table.
////But I want only one value from one cell, suppose from first row and third column
}
}
As you are saying if your td cell value is 2 and no other cell has value 2 then try this xpath :-
//td[text() = '2']
or
//tr[1]/td[text() = '2']
And if you want to get cell value try as below (Amusing you are using java) :-
WebElement table= driver.findElement(By.id("table"));
String cellValue = driver.findElement(By.xpath("//table/tr[1]/td[3]")).getText()
Edited :- you should try to get text from first row third column as below :-
String thirdColumnText = table.findElement(By.tagName("tr")).get(0).findElements(By.tagName(td)).get(3).getText();
Note :- For more better solution, need to share your table HTML
hope it helps..:)
I have created an excel containing multiple rows, each row corresponding to a test case. The Excel has around 22 columns (parameters) including a "Flag" column.
I want the Dataprovider to return only those columns which has a value 'Y' in the excel column. The use case is that when the client wants to run a particular test case, they only need to flag it to Y or N. How can I achieve this is in TestNG with Selenium?
My colleague had helped me to achieve this using following code, but this does not work as per my new code structure.
#DataProvider(name = "Order")
public Object[][] menu()
{
Object[][] data = UtilLibrary.getData("Order");
int intColCount = UtilLibrary.datatable.getColumnCount("Order");
int j = 0;
int arrRowCount=0;
for (int i = 0; i < data.length; i++) {
if((data[i][intColCount-1]).equals("Y"))
{
arrRowCount++;
}
}
j=0;
Object[][] retData = new Object[arrRowCount][intColCount];
for (int i = 0; i < data.length; i++)
{
if ((data[i][intColCount-1]).equals("Y")) {
retData[j] = data[i]; j++; }
}
return retData;
}
The above code sends only the record/s having flag='Y' in the excel to the Dataprovider. But, it was working only when the test script had a single #Test method having all automation steps, while now I have multiple #Test methods to simulate the same steps to which I have passed this same Dataprovider (Order).
Let me know if someone has achieved this using a similar code or if TestNG has a specific feature to send filtered rows of excel to the Dataprovider
I have now fixed the issue. The reason why the above given code for rows having flag = 'Y' in Excel was not working with multiple #Test methods was only due to a silly mistake. The mistake was that I had changed the excel structure a bit and did not modify the above code as per the index of the new flag column.
How do I increment value of img path when said path looks like this?
//ab[x]/img
X value increasing by 1 and has a limit of 50.
Trying to write a test case on how to click on several images on website.
Edit: Just wanted to add that I'm just starting with Selenium IDE and using standart commands.
Solution 1: Format your xpath path selector
for(int i=1; i<=numberOfImages; i++) {
String path = String.format("//ab[%d]/img", i);
WebElement image = driver.findElement(By.xpath(path));
if(image != null) {
image.click();
}
}
Solution 2: Select all elements that "//ab/img" returns and iterate over them.
String path = "//ab/img";
List<WebElement> imgElements = driver.findElements(By.xpath(path)); //notice the plural
for(WebElement image : imgElements) {
image.click();
}
I have an ArrayList ArrayList<String> externalDataList = new ArrayList<>(1600);and I would like to display in a textArea first 3 strings, but I can't succed:
Here is my code
textareaShowPreview.setPrefRowCount(3);
Iterator<String> it = externalDataList.iterator();
int tot = 0;
while(it.hasNext() && tot<3){
String element = it.next();
textareaShowPreview.setText(element + "\n");
System.out.println("elements are: " + element);
tot++;
}
The sout correctly print first 3 strings
element are: 23/05/2007 ,30.9455,31.2545,30.9091,30.9545,7518142
element are: 24/05/2007 ,30.6545,31.0909,30.5364,30.6909,12851606
element are: 25/05/2007 ,30.6636,30.8545,30.4818,30.8091,9392088
but in textArea I have only first one
How do I have to modify my code to show in textArea all three strings, one string per row?
Use appendText instead of setText here is a link.
The setText, delete the previous text and set the text you are giving to it. The append keep the current text in your text area.
Hope it helps!