2011-04-28 12 views
8

सेलेनियम का उपयोग कर स्वचालन टेसिंग कर रहा हूं, मुझे रेडियो बटन का चयन करने के तरीके में सहायता चाहिए। अगर संभवतः सेलेनियम जावा कोड के साथ मेरी मदद करें।रेडियो बटन का चयन करने के लिए सेलेनियम कोड

+0

फेंकता कैसे आप के लिए Tnem काम द्वारा प्रदान का उत्तर दिया? अगर सही है तो इसे स्वीकार करने की देखभाल करें? –

उत्तर

7

मान लिया जाये कि आप सेलेनियम इसकी अभी सेट है:

selenium.click('radio button locator'); 

आप सेलेनियम जावाडोक http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/java/com/thoughtworks/selenium/Selenium.html

+4

सेलेनियम डॉक पृष्ठ का उल्लेख किया एक अच्छी शुरुआत है, लेकिन लोकेटर पर अधिक व्यापक संदर्भ के लिए देख अपनी दीवार चार्ट: [XPath, सीएसएस, डोम और सेलेनियम: रोसेटा स्टोन और कुकबुक] (http://www.simple-talk.com /dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/) –

0
public class radioExamJavaScr { 

    public static void main(String[] args) throws IOException { 

    WebDriver driver = new FirefoxDriver(); 
    EventFiringWebDriver dr = new EventFiringWebDriver(driver); 
    dr.get("http://www.makemytrip.com/"); 
    dr.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

    ((JavascriptExecutor)dr).executeScript("document.getElementById('roundtrip_r').click();"); 

    WebElement one_way = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('oneway_r') ;"); 

    System.out.println(one_way.isSelected()); 

    WebElement round_trip = (WebElement)((JavascriptExecutor)dr).executeScript("return document.getElementById('roundtrip_r') ;"); 

    System.out.println(round_trip.isSelected()); 

     } 
} 

को देखने के लिए ऊपर के उदाहरण के साथ मैं रेडियो बटन का चयन कर रहा हूँ में चाहते हो सकता है "राउंड ट्रिप "जावास्क्रिप्ट" का प्रयोग कर रहे हैं।

अंतिम चार पंक्तियां सत्यापित करने और देखने के लिए हैं कि पृष्ठ में अपेक्षित रेडियो बटन चुना गया है या नहीं।

नोट: मैं चुनिंदा वेबपृष्ठ में समस्या (एक रेडियो का चयन) को हल करने के लिए सरल आसान समाधान दे रहा हूं। एक बेहतर कोड लिखा जा सकता है। (उपयोगकर्ता सभी मौजूदा रेडियो बटन के माध्यम से रेडियो आईडी और लूप को स्वीकार करने के लिए एक विधि लिख सकते हैं यह देखने के लिए कि उनमें से कौन सा चुना गया है)।

+0

ऊपर समाधान बेहतर काम करता है, तो उपयोगकर्ता असमर्थ रेडियो बटन का चयन और "driver.findElement की मदद से सत्यापित करने के लिए ("") " तरीका। मैं कुछ मामलों पता जहां "driver.findElement (" ")" और इसकी उम्मीद काम करने में असमर्थ क्रिया (न चयन और न ही सत्यापित करें)। JavaScriptExecutor किसी भी परीक्षक के लिए दूसरा विकल्प है। – MKod

1
click > xpath=(//input[@type='checkbox'])[position()=1] 
click > xpath=(//input[@type='checkbox'])[position()=2] 
click > xpath=(//input[@type='checkbox'])[position()=3] 
click > xpath=(//input[@type='checkbox'])[position()=4] 

आदि ... उपयोग इस यादृच्छिक और किसी भी रेडियो बटन का चयन करने के लिए

0

मैं इस विधि का उपयोग आदेश:

String radioButtonId = "radioButtonId"; 
selenium.focus("id=" + radioButtonId); 
selenium.click("id=" + radioButtonId, "concreteRadioButtonValue"); 
0

इस आप क्या कर सकते है:

बनाएं WebElement रिटर्न प्रकार के साथ एक विधि, और विधि findElement() का उपयोग करें। उदाहरण:

WebDriver test; 
test = new FirefoxDriver(); 

public static WebElement checkAndGet(By b) throws Exception { 
    return test.findElement(b); 
    } 

वेबलेमेंट स्टोर करें और विधि क्लिक() का उपयोग करें। उदाहरण:

WebElement radiobutton = checkAndGet(By.xpath("//span[@class='label ng-binding']"); 
radiobutton.click(); 

आशा है कि इससे मदद मिलती है!

-1

आपको हर समय क्या करना चाहिए पृष्ठ के भीतर प्रत्येक ऑब्जेक्ट के लिए लोकेटर प्रदान करना है और फिर एक विधि का उपयोग करना है।

तरह

driver.findElement(By.xpath(//CLASS[contains(., 'what you are looking for')])).click();

0

टेस्ट परिदृश्य: लिंग का चयन करें (महिला) रेडियो बटन कदम:

  1. लॉन्च नई ब्राउज़र
  2. ओपन यूआरएल http://toolsqa.wpengine.com/automation-practice-form/

  3. का चयन करें रेडियो बटन (महिला) मान 'महिला'

कोड द्वारा: public static void (String [] args) InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe"); 
    WebDriver driver = new ChromeDriver(); // Step 1 - Launch Browser 
    driver.get("http://toolsqa.com/automation-practice-form"); // Step 2 - Open Test URL 
    List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex")); // 
    int size = rdBtn_Sex.size(); 
    System.out.println("Total no of radio button :"+size); 
    for (int i=0; i< size; i++) 
    { 
     String sValue = rdBtn_Sex.get(i).getAttribute("value"); // Step 3 - 3. Select the Radio button (female) by Value ‘Female’ 
     System.out.println("Radio button Name "+sValue); 
     if (sValue.equalsIgnoreCase("Female")) 
     { 
      rdBtn_Sex.get(i).click(); 

     } 
    } 
संबंधित मुद्दे