使用selenium参数化选择选项

时间:2014-03-31 11:50:05

标签: java unit-testing selenium selenium-webdriver

我有一个场景,我必须选择一个行业,并根据行业选择我必须选择一个类别,并根据类别我必须选择分销商。

虽然我能够选择行业和类别,但我无法选择我的经销商。我已经传递了参数。

我的junit代码是

@RunWith(Parameterized.class)
     public class test2 {

       WebDriver driver;
       private String email; 
       private String pdesc;
       private String pass;
       private String ind;
       private String cat;
       private String dis;
       private String pname;
       private String at1;
       private String atv1;
       private String img;

       public test2(String emailid, String password,String industry, String cate,  
                        String dist, String productname, String productdesc, 
                             String attname, String attvalue,String uploadimg ){
        this.email = emailid;
        this.pass = password;
        this.ind = industry;
        this.cat = cate;
        this.dis =dist;
        this.pname = productname;
        this.pdesc = productdesc;
        this.at1 = attname;
        this.atv1 = attvalue;
        this.img = uploadimg;
          }


     @Parameters
     public static Collection<Object[]>data(){     
     Object[][] data = new Object[][] 
     { {"user1","user1","Industry1","category1","distributor","product name",
     "product description","price","Rs. 1,50,000","Lighthouse.jpg" }  };

      return Arrays.asList(data);
     }

    @Before
    public void setUp(){
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    }

    @After
    public void tearDown(){
    driver.quit();
    }


@Test
    public void testFindElements()throws Exception{
    driver.get("http://domain.com");
    Thread.sleep(500);

  *****Login with username and password******

    WebElement em = driver.findElement(By.id("email_id"));
    em.sendKeys(email);
    WebElement pwd = driver.findElement(By.id("password"));
    pwd.sendKeys(pass);
    Thread.sleep(500);
    driver.findElement(By.xpath("//button[@type='submit']")).click();
    Thread.sleep(500);

    *******NAVIGATE TO ADD PRODUCT PAGE*******

    WebElement hover = driver.findElement(By.className("dropdown-toggle"));
    Actions builder = new Actions(driver);
    builder.moveToElement(hover).build().perform();
    driver .findElement(By.linkText("Add")).click();

    ****SELECTING THE INDUSTRY****
    new Select(driver.findElement(By.id("ProductIndustryId"))).selectByVisibleText(ind);   

    *****SELECTING THE CATEGORY*********
    WebElement category = driver.findElement(By.id("ProductCategoryId"));
    category.sendKeys(cat);

    ****CLICK ON SELECT DISTRIBUTOR RADIO BUTTON*****
    driver.findElement(By.cssSelector("div.radio-list > label")).click();
    driver.findElement(By.id("seldist")).click();
    Thread.sleep(500);
    Thread.sleep(500);

    ****SELECTING MY DISTRIBUTOR(JOHN)*****
    driver.findElement
   (By.xpath("//html/body/div[3]/div[2]/div/div/form/div[3]/div/div/div/div/div[5]/div/div/div/select/option")).sendKeys(dis);

    ****ADDING PRODUCT DETAILS****

    WebElement prodname = driver.findElement(By.cssSelector("div.col-md-9 > 
    input[name=\"data[Product][product_name]\"]"));
    prodname.sendKeys(pname);

    WebElement pd = driver.findElement(By.cssSelector("textarea[name=\"data[Product]
    [description] rows=\"]"));
    pd.sendKeys(pdesc);

    WebElement a1 = driver.findElement(By.name("data[Attribute][0][display_name]"));
    a1.sendKeys(at1);

    WebElement av1 = driver.findElement(By.name("data[Attribute][0][product_value]"));
    av1.sendKeys(atv1);

    driver.findElement(By.name("data[Product][product_image]")).click();
    WebElement upload = driver.findElement(By.name("data[Product][product_image]"));
    upload.sendKeys(img);

    driver.findElement(By.cssSelector("div.col-md-offset-3.col-md-9 > 
                                button.btn.green")).click();

    driver.findElement(By.linkText("Done")).click();
       } 
    }


 <select name="data[Product][industry_id]" class="form-control" id="ProductIndustryId">
 <option value="">-Select Industry-</option>
 <option value="1">Textiles</option>
 <option value="2">Automobiles</option>
 <option value="10">Home Improvement</option>
 </select>
 <select name="data[Product][category_id]" class="form-control" id="ProductCategoryId">
 </select>
 <label>
 <div class="radio" id="uniform-seldist"><span class="checked"><input type="radio" 
 name="data[Product][user_type]" id="seldist" value="A" style="display:inline">
 </span>
 </div>
 Select distributor</label>
 <div class="input select">
 <input type="hidden" name="data[Product][distributor_id]" value=""     
    id="ProductDistributorId_">
 <select name="data[Product][distributor_id][]" class="form-control" multiple="multiple" 
    id="ProductDistributorId">
 </select></div>

注意:我无法选择参数中传递的分配器。但选择其他经销商。例如:经销商名单是john,Dennis,Katie。

我已通过 john 成为我的经销商,但它选择 katie

1 个答案:

答案 0 :(得分:0)

@ user3181335我不得不写这个作为答案,因为我没有足够的代表评论。在执行每个操作后尝试包含一些JUnit断言。这将帮助您识别可能遗漏的错误,例如选择Katie而不是John的情况。

WebElement selectDistributor = driver.findElement(By.xpath("//html/body/div[3]/div[2]/div/div/form/div[3]/div/div/div/div/div[5]/div/div/div/select/option")).sendKeys(dis);

//this is an example without seeing the HTML code
Assert.assertEquals("Expecting my distributor to be John", selectDistributor.getValue("john");