2017-03-03 1 views
-1

날짜 선택 도구를 site으로 테스트하려고합니다. 선택한 날짜로 텍스트 필드를 채우게하는 날짜를 선택했습니다.셀레늄을 사용하여 datepicker를 통해 채워지는 값을 캡처하고 어설 션하는 방법

이제 선택한 날짜가 예상 한대로 유효한지 확인하고 싶습니다. 날짜를 선택하고 텍스트 필드를 채울 수있었습니다.

하지만 값의 유효성을 검사 할 때 텍스트 필드의 공백이 다시 나타납니다.

날짜 선택 도구로 채워진 값을 캡처하고 같은 값을 지정할 수 있습니까?

나는 아래에서 언급 한 코드를 수행했지만 도움이되지 않았다.

driver.get(" https://www.jqueryui.com"); 
driver.findElement(By.linkText("Datepicker")).click(); 
WebElement element1 = driver.findElement(By.className("demo-frame")); 
driver.switchTo().frame(element1); 
driver.findElement(By.xpath(".//*[@id='datepicker']")).click();   
driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click(); 
List<WebElement> element = driver.findElements(By.xpath(".//*[@id='ui-datepicker-div']/table/tbody/tr/td")); 
int count = element.size(); 
for (int i = 0; i < count; i++) 
{ 
    String data = element.get(i).getText(); 
    if("2".equals(data)) 
    { 
     element.get(i).click(); 
     break; 
    } 
} 

driver.findElement(By.xpath(".//*[@id='datepicker']")).click(); 
WebElement element2 = driver.findElement(By.xpath(".//*[@id='datepicker']")); 

String value = element2.getText(); 
Assert.assertEquals("04/02/2017",value); 

내가 예상 한 값은 04/02/2017이며 2가 선택됩니다. 분명히 DOM은 업데이트되지 않습니다. 그렇다면 선택된 값을 캡처하고 주장하기 위해 여기서 무엇을해야합니까?

답변

0

선택한 날짜의 가치를 얻으려면 JavascriptExecutor을 사용하십시오. 다음 코드는 귀하의 경우에 효과가 있습니다.

WebElement element2 = driver.findElement(By.xpath(".//*[@id='datepicker']")); 
JavascriptExecutor jse = (JavascriptExecutor)driver; 
String date = (String) jse.executeScript("return arguments[0].value", element2); 
System.out.println("Date:- " + date); 
Assert.assertEquals("04/02/2017", date); 

감사!

0

사용 driver.findElement(By.id("datepicker")).getAttribute("value");

 driver.get(" https://www.jqueryui.com"); 
     driver.findElement(By.linkText("Datepicker")).click(); 
     WebElement element1 = driver.findElement(By.className("demo-frame")); 
     driver.switchTo().frame(element1); 
     driver.findElement(By.xpath(".//*[@id='datepicker']")).click(); 
     driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click(); 
     List<WebElement> element = driver.findElements(By.xpath(".//*[@id='ui-datepicker-div']/table/tbody/tr/td")); 
     int count = element.size(); 
     for (int i = 0; i < count; i++) { 
      String data = element.get(i).getText(); 
      if ("2".equals(data)) { 
       element.get(i).click(); 
       break; 
      } 
     } 

     String value=driver.findElement(By.id("datepicker")).getAttribute("value"); 
     System.out.println("value is" +value); 
     Assert.assertEquals("04/02/2017", value); 
1

설명 같은 것을 시도해보십시오 사용 .getAttribute()date selection의 가치를이 코드 아래 참조에 대한 자세한 내용은 value 등의 속성을 전달합니다.

설명은 아래 이미지를 참조하십시오.

enter image description here

코드 아래에이보십시오, 나는 array method를 사용하여이 솔루션을 했어요.

driver.get("https://jqueryui.com/datepicker/"); 
driver.manage().window().maximize(); 
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 

WebElement iframe = driver.findElement(By.tagName("iframe"));  //Move inside iframe. 
driver.switchTo().frame(iframe); 
Thread.sleep(2000); 

WebElement date_texbox = driver.findElement(By.id("datepicker"));  
date_texbox.click();            //Date-picker text-box element 
Thread.sleep(2000); 
WebElement date = driver.findElement(By.xpath("//a[@title='Next']/span")); 
date.click();              //Move to the April Month 2017 from date picker. 

int[] array_date = new int[]{1,2,3,4,5,6,7,8}; //Create int array for dates. 

int k = 1;          //K refers to tr tag 
int l = 7;          //l refers to td tag 
for(int j=0;j<array_date.length;j++) 
{ 
    if(l==8) 
    { 
     k++; 
     l=1;   
    } 

    if(k==2 & l==1) 
    { 
     System.out.println("Date = 04/02/2017"); 
     String second_april_2017 = driver.findElement(By.xpath("//table/tbody/tr[2]/td[1]/a")).getText(); 
     System.out.println(second_april_2017); 

     driver.findElement(By.xpath("//table/tbody/tr["+k+"]/td["+l+"]/a")).click(); 

     String get_date_value = driver.findElement(By.id("datepicker")).getAttribute("value"); 
     System.out.println("Date Selection Value = " +get_date_value); 
     Assert.assertEquals("04/02/2017", get_date_value);       //verify condition for this 04/02/2017 date. 
    } 

    System.out.println(k); 
    System.out.println(l); 

    if(!(k==2 & l==1))  //if date selection is 04/02/2017 then this condition will not execute. 
    { 
     driver.findElement(By.xpath("//table/tbody/tr["+k+"]/td["+l+"]/a")).click();  // pass array value of k and l to the xpath. 
    } 

    Thread.sleep(2500); 
    date_texbox.click(); 
    Thread.sleep(2500); 
    l++; 
} 

driver.switchTo().defaultContent(); //Move out side to the frame. 
관련 문제