Selenium Java, How to check if multiple radio buttons are selected? - java

I am working on a framework and have tried the following and not results :
List<WebElement> rows = EarningNormal.oRdioList;
java.util.Iterator<WebElement> i = rows.iterator();
while(i.hasNext()) {
WebElement ContribIDYES = i.next();
//System.out.println(sitcodes.getText());
if(ContribIDYES.isSelected()){
TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES","Contribution ID's are set to YES" , "PASS", "Done");
}
else{
TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES", "Contribution ID's are NOT seto to YES as default", "FAILED",TestDriver.comUtil.getImageFileLoc(TestDriver.globalProps.getWebDriver()));
}
}

Hi when you want to verify if a radio button is selected or not then please pay attention that any input tag with type radio has a hidden attribute value known as selected if a radio button is selected then its value is = True and if not then its value is = null,hence on the basis of this you can easily identify which radio button is selcted or not .below find a working example for the same
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("C:\\Users\\rajnish\\Desktop\\myradio.html");
// working with radio buttons
// first take all the radio buttons inside a list like below
List<WebElement> myradioloc = driver.findElements(By.xpath("//*[#type='radio']"));
// apply the for loop to identify/ verify if a radio button is selected or not
for(int i=0;i<myradioloc.size();i++){
System.out.println("attribut value Selected of radio button is : " + myradioloc.get(i).getAttribute("selected"));
// if radio button is selected then value of selected attribute is True else null
if( myradioloc.get(i).getAttribute("selected").equals("null")){
// as if loop will only run when value of selected attribute is null
// i.e only when radio button is not selected
myradioloc.get(i).click();
}
}

Related

Why I can't update my database when saving data from 10 radioButtons?

I have 10 toggleGroup Buttons with 4 radio buttons each. When you click one radio button within a toggle group, It will add the index value of the selected radio button to an Arraylist. THen I have a "save" button to save those values to a database.
public ArrayList<Integer> RightInt(){
for(ToggleGroup tg: toggleRDB()) {
tg.selectedToggleProperty().addListener((observable, oldValue, newValue) ->{
if(newValue != null) {
//adds the index of the selected RadioButton to selectedRDBIndex list
selectedRDBIndex.add(tg.getToggles().indexOf(newValue));
}
});
}
return selectedRDBIndex;
}
//selectedRDBIndex is an ArrayList<Integer> storage.
The code above returns a list of Integer with 10 values.
This code is the action performed by the save button.
for(int i = 0; i < 10; i++) {
//answerStore.storeRDB connects to my dataBase to save the values returned by the RightInt() function.
answerStore.storeRDB(i+1, TB, RightInt().get(i));
}
RightInt().clear();
The code above works well. It saves and updates values to my database.
Then I decided to use those values again here:
for(int i = 0; i<10 ; i++) {
toggleRDB().get(i).getToggles().get(answerStoration.retrieveDataRDBSet(i+1,TB)).setSelected(true);
}
//toggleRDB() returns a list of toggleGroups.
//answerStoration.retrieveDataRDBSet gets the radioButton integer data in the database that i used to setSelect an index of radioButton in each toggle group.
I used all these codes so that when the user selected a radioButton that progress will be saved. But after using the code above I can't update datas in my database. I hope you understood my situation and solve this problem. THanks in advance.
I got some hints on my problem. When I click a radio button
It adds another place on the arraylist that's why the values aren't changing. Now I need figure out where to place a set () method or how to replace those values. I would like to say that clearing the list is not an option.

how to setChecked radio button in programmatically (radio button created in dynamically ) android

I try that
radioButton.setChecked(true);
but its only work 4th radio button. I try to create radio button dynamically. I create radio buttion within for loop, then store the radio button value. Then restore the radio button value (that means, I have 4 options that time I choose 2nd option and saved it then restore it(setChecked 2nd option) ) but its only setChecked 4th option.
Create radioButton.
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
radioButton = new RadioButton(getContext());
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
Try to restore that
if(choiceElementList.get(k).getId() == Cons.Id){
radioButton.setChecked(true);
}
First set Ids to your RadioButtons
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
RadioButton radioButton = new RadioButton(getContext());
// Set ID to Radio Button
radioButton.setId(k);
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
now just use your RadioGroup to check desire RadioButton with its ID
if(choiceElementList.get(k).getId() == Cons.Id){
radioGroup.check(k); // K will be your ID Set for your desire RadioButton
}
Happy Coding...
according to this code segments, your radioButton variable only refer last created element (radiobutton). That's why it only marks fourth one. You need to get correct reference for radio button what u want.
This is because you're adding all your RadioButton in a RadioGroup. When a RadioButton is checked in a RadioGroup, the othe RadioButton will be unchecked. You can see the RadioGroup documentation say it clearly:
This class is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.
Intially, all of the radio buttons are unchecked. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.
The selection is identified by the unique id of the radio button as defined in the XML layout file.

How to check radio button for gender selection in selenium webdriver?

I want to click on radio button, if radio button is already selected then it should skip for the selection.
For more details on this visit this site, go to the Signed in option -> Create an account, then you will be able to getting the page, that is provided to the screenshot.
Refer Image:
Please Find the workable code:
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://automationpractice.com/index.php");
Thread.sleep(2000);
driver.findElement(By.xpath(".//a[#class='login']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath(".//input[#id='email_create']")).sendKeys("abc123_1#yahoo.com");
Thread.sleep(1000);
driver.findElement(By.xpath(".//button[#id='SubmitCreate']")).click();
Thread.sleep(4000);
driver.findElement(By.xpath(".//input[#id='id_gender1']")).click();
Thread.sleep(1000);
You can put implicit waits as well
Try this way to check that the MR radio button is selected or not? If MR radio button is not selected then only if condition will execute.
WebElement MR = driver.findElement(By.xpath("//div/span/input[#id='id_gender1']"));
if(!MR.isSelected())
{
MR.click();
}
OR
If you want to check that the Mrs radio button is selected or not? If Mrs radio button is not selected then only if condition will execute.
WebElement Mrs = driver.findElement(By.xpath("//div/span/input[#id='id_gender2']"));
if(!Mrs.isSelected())
{
Mrs.click();
}

Find checkbox and add text

I would like to add in the secondary text field at this website a text string.
My selenium method looks like the following:
public void addSecondaryText(String string) {
//click secondary button
WebElement secButton = driver.findElement(By.xpath("//label/input[#id='sub-text-check']"));
if (secButton.isDisplayed()) {
secButton.click();
}
//clear text
WebElement secTextField = driver.findElement(By.xpath("//*[#id='sub-text']"));
secTextField.clear(); // I get the exception here!
//add text
secTextField.sendKeys(string);
}
However, I currently get an exception for secTextField.clear();, that it cannot be manipulated:
Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state: Element is not currently interactable and may not be manipulated
Any suggestions what I am doing wrong?
I appreciate your replies!
The issue here is that the textarea depends on another button and you need to perform a click on it before making the textarea editable. See the image below.
//List will help of to determine the "Add secondary text" button needs to be clicked on not
//if the count is greater than 0 then click it
IList<IWebElement> elements = driver.FindElements(By.XPath("//span[contains(text(),'Add secondary text')]"));
if (elements.Count>0)
{
elements.FirstOrDefault().Click();
}
IWebElement element = driver.FindElement(By.Id("sub-text"));
element.Clear();
element.SendKeys("Test");
Following code should work -
public void addSecondaryText(String string) {
//click secondary button
WebElement secButton = driver.findElement(By.xpath("//span[#class='sub-text-toggle']"));
secButton.click();
//clear text
WebElement secTextField = driver.findElement(By.xpath("//*[#id='sub-text']"));
secTextField.clear(); // I get the exception here!
//add text
secTextField.sendKeys(string);
}

How To Perform Multiple Radio Button Click in Selenium Webdriver

How to make condition to perform multiple radio button click (see image below)...?
multiple radio button click
I've tried for hours, but I got only single radio button click (see attached image)
single radio button click
List<WebElement> radiobutton = driver.findElements(By.xpath("//*[#type='radio']"));
System.out.println("Total element is " + radiobutton.size());
for (int i = 0; i < radiobutton.size(); i++) {
// if you are getting stale element exception because of the page
// reload
radiobutton = driver.findElements(By.xpath("//*[#type='radio']"));
System.out.println(radiobutton.get(i).getAttribute("value"));
// select your radio and click to go to next page
radiobutton.get(i).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//input[#id='btnCheckFare']"))
.click();
Thread.sleep(3000);
}
I saw your video.You have multiple groups of radio button and you can select one radio button in each group.
You have to use 2 loops,one for group and one inner loop for select radio button in a group.
If you have experience in programming then you can do it easily.Otherwise provide the link where we can see this functionality.

Categories