2011-12-04 20 views
7

Chcę moje selen badanie IDE przypadek uruchomić jak kroki poniżej, aby wybrać datę się automatycznie:Selen IDE: Jak wybrać następny wolny termin od datepicker

  • datę Kliknij wyjazdu otworzyć datepicker
  • począwszy od dnia aktualnie wybrany, loop Through datami aż do następnej dostępnej datę (w razie potrzeby przejść do następnego miesiąca lub roku, aby znaleźć następny wolny termin)
  • Wybierz dostępną datę z datepicker

Czy ktoś może mi pokazać, że jestem nowy w selenach, jak to zrobić w powyższym przykładzie? Cały mój skrypt może w tej chwili otworzyć kalendarz.

Poniżej jest html udało mi się otrzymać, że mecze z powyższym zrzucie ekranu:

//Months drop down 
    <select class="ui-datepicker-month" data-handler="selectMonth" data-event="change"> 
    <option value="2" selected="selected">Mar 
    </option><option value="3">Apr</option> 
    <option value="4">May</option> 
    <option value="5">Jun</option> 
    <option value="6">Jul</option> 
    <option value="7">Aug</option> 
    <option value="8">Sep</option> 
    <option value="9">Oct</option> 
    </select> 
//Years drop down 
    <select class="ui-datepicker-year" data-handler="selectYear" data-event="change"> 
    <option value="2016" selected="selected">2016</option> 
    </select> 
    <table class="ui-datepicker-calendar"> 
//days labels 
    <thead> 
    <tr> 
    <th scope="col"><span title="Monday">Mo</span></th> 
    <th scope="col"><span title="Tuesday">Tu</span></th> 
    <th scope="col"><span title="Wednesday">We</span></th> 
    <th scope="col"><span title="Thursday">Th</span></th> 
    <th scope="col"><span title="Friday">Fr</span></th> 
    <th scope="col" class="ui-datepicker-week-end"><span title="Saturday">Sa</span></th> 
    <th scope="col" class="ui-datepicker-week-end"><span title="Sunday">Su</span></th> 
    </tr> 
    </thead> 
    <tbody> 
//dates 
    <tr> 
    <td class=" ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">1</span></td> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">2</span></td> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">3</span></td> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">4</span></td> 
    <td class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">5</span></td> 
    <td class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">6</span></td></tr> 
    <tr> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">7</span></td> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">8</span></td> 
    ...same process till last week of dates (bottom row of calendar in screenshot) 
    <tr> 
    <td class=" ui-datepicker-days-cell-over ui-datepicker-current-day" title="Click to see flights on this date" data-handler="selectDay" data-event="click" data-month="2" data-year="2016"><a class="ui-state-default ui-state-active" href="#">28</a></td> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">29</span></td> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">30</span></td> 
    <td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">31</span></td> 
    <td class=" ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td><td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td> 
    <td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td> 
    </tr> 
    </tbody> 
    </table> 
+0

[Proszę nie wpadać na Twoje pytania, zamieszczając nowe pytanie jak prośbą o uwagi] (http: // stackoverflow.com/questions/35241704/selecting-dates-from-a-datepicker-selenium-ide). Twoja nagroda to więcej niż potrzeba. – cybermonkey

+1

Dlaczego używasz "Selenium IDE", a nie żadnych powiązań językowych selenu z webdriverem? Czy byłbyś w porządku z odpowiedzią opartą na python-selen? – alecxe

+0

@alecxe To dlatego, że nasza firma używa głównie selenu ide. W rzeczywistości jestem maniakiem ręcznym, ale chcę nauczyć się trochę automatyzacji, dzięki czemu mogę testować znacznie szybciej. Jeśli Python-Selen pomoże selenowi ide, to tak, idź do niego. Praktycznie po prostu muszę znać polecenia, jak to zrobić. Wydaje mi się, że potrzebuję zrobić storeeval i zapętlić się przez kilka dni, aż znajdę następną dostępną datę, ale po prostu nie mogę wymyślić, jak to zrobić. – BruceyBandit

Odpowiedz

4

automatyzację tego zadania w Selenium IDE byłoby dość wyzwanie ponieważ istnieje nietrywialne logika zaangażowany w otrzymując następną dostępną datę, należy rozważyć wybór jednego z dostępnych wiązań języka selenowego: switching to Selenium WebDriver.

Oto działający kod utworzony przy użyciu Python language Selenium bindings. Chodzi o to, aby:

  • instancji WebDriver (używając Firefox() w tym przykładzie, ale są też inne opcje też)
  • zmaksymalizować okno przeglądarki
  • nawigować do adresu URL (http://www.jet2.com)
  • czekać, aż strona jest załadowana jawnie (docs)
  • wypełnij pola odlotu i docelowego
  • kliknij pole "Wylot", aby wyświetlić kalendarz, który ma być wyświetlany
  • w kalendarzu zlokalizuj aktualną datę - ma ui-datepicker-current-day klasy
  • sprawdzenie, czy jest dostępna data w tym miesiącu za pomocą „przez XPath” techniki lokalizacji i following axis. Jeśli tak, wydrukuj i kliknij.
  • jeśli nie znaleźliśmy następnej dostępnej daty w tym miesiącu, zainicjuj i "niekończącą się" pętlę i kliknij przycisk "Następny" miesiąc sprawdzając, czy mamy dostępną datę. Wydrukuj i kliknij, jeśli znajdziesz i wyjdź z pętli. W przeciwnym razie kliknij przycisk "Dalej".
  • jeśli nie mamy przycisk „Dalej” - to jesteśmy pod koniec roku, należy wybrać w przyszłym roku w roku rozwijanej kontynuować pętlę
  • zamknąć sterownik po zakończeniu

Kod:

from selenium import webdriver 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver import ActionChains 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.select import Select 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 


FROM = "Leeds Bradford" 
TO = "Budapest BUD" 

driver = webdriver.Firefox() # or, webdriver.Chrome(), or webdriver.PhantomJS() or etc. 
driver.maximize_window() 
driver.get("http://www.jet2.com") 

wait = WebDriverWait(driver, 10) 
actions = ActionChains(driver) 

# wait for the page to load 
wait.until(EC.presence_of_element_located((By.ID, "departure-airport-input"))) 

# fill out the form 
driver.find_element_by_id("departure-airport-input").send_keys(FROM) 
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ui-id-1 .ui-menu-item"))).click() 

driver.find_element_by_id("destination-airport-input").send_keys(TO) 
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ui-id-2 .ui-menu-item"))).click() 

# select date 
datepicker = driver.find_element_by_id("departure-date-selector") 
actions.move_to_element(datepicker).click().perform() 

# find the calendar, month and year picker and the current date 
calendar = driver.find_element_by_id("departureDateContainer") 
month_picker = Select(calendar.find_element_by_class_name("ui-datepicker-month")) 
year_picker = Select(calendar.find_element_by_class_name("ui-datepicker-year")) 
current_date = calendar.find_element_by_class_name("ui-datepicker-current-day") 

# printing out current date 
month = month_picker.first_selected_option.text 
year = year_picker.first_selected_option.text 
print("Current date: {day} {month} {year}".format(day=current_date.text, month=month, year=year)) 

try: 
    # see if we have an available date in this month 
    next_available_date = current_date.find_element_by_xpath("following::td[@data-handler='selectDay' and ancestor::div/@id='departureDateContainer']") 
    print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year)) 
    next_available_date.click() 
except NoSuchElementException: 
    # looping over until the next available date found 
    while True: 
     # click next, if not found, select the next year 
     try: 
      calendar.find_element_by_class_name("ui-datepicker-next").click() 
     except NoSuchElementException: 
      # select next year 
      year = Select(calendar.find_element_by_class_name("ui-datepicker-year")) 
      year.select_by_visible_text(str(int(year.first_selected_option.text) + 1)) 

     # reporting current processed month and year 
     month = Select(calendar.find_element_by_class_name("ui-datepicker-month")).first_selected_option.text 
     year = Select(calendar.find_element_by_class_name("ui-datepicker-year")).first_selected_option.text 
     print("Processing {month} {year}".format(month=month, year=year)) 

     try: 
      next_available_date = calendar.find_element_by_xpath(".//td[@data-handler='selectDay']") 
      print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year)) 
      next_available_date.click() 
      break 
     except NoSuchElementException: 
      continue 

driver.close() 

wyniki badań:

  • Leeds Bradford -> Antalya AYT (następny dostępny data w kwietniu):

    Current date: 28 Mar 2016 
    Processing Apr 2016 
    Found an available date: 4 Apr 2016 
    
  • Leeds Bradford -> Budapeszt BUD (następna data dostępne w tym samym miesiącu, bieżącej daty):

    Current date: 12 Feb 2016 
    Found an available date: 15 Feb 2016 
    
  • ? (następna dostępna data w następnym roku)

+0

Selen w pytonie wygląda naprawdę fajnie. Nauczę się tego w wolnym czasie w domu z twoją odpowiedzią i dokumentami. Jednak teraz wiem, dlaczego używamy selenu ideowego, ponieważ szybciej jest po prostu nagrać nasze ręczne testy. Kalendarz jest jedyną przeszkodą, jeśli chodzi o potrzebę zrobienia czegoś fantazyjnego w tle, ale z samym IDE, po prostu rejestrujemy nasze ręczne testy. Odpowiedź na Awans jest bardzo dobra, ale musi to być tylko IDE. Nie będzie miał czasu na wpisywanie skryptów Pythona podczas wykonywania testów ręcznych w miejscu pracy. – BruceyBandit

0

Oto wersja Java.

import java.util.List; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class DatePicketJet2 { 
    public static WebDriver browser; 

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

     browser = new FirefoxDriver(); 
     String url = "http://www.jet2.com/"; 

     browser.get(url); 
     browser.manage().window().maximize(); 

     browser.findElement(By.id("departureAirportList")).click(); 
     WebElement departureCountry = browser.findElement(By.id("destinations-uk")); 
     WebElement departureCityLocator = departureCountry.findElement(By.tagName("ul")); 
     List<WebElement> departureCities = departureCityLocator.findElements(By.tagName("li")); 
     /* 
     for (WebElement we : departureCities) { 
      System.out.println(we.getText()); 
     } 
     */ 
     departureCities.get(1).click(); 

     browser.findElement(By.id("destinationAirportList")).click(); 

     WebElement destinationCountry = browser.findElement(By.id("destinations-country")); 
     WebElement destinationCityLocator = destinationCountry.findElement(By.tagName("ul")); 
     List<WebElement> destinationCities = destinationCityLocator.findElements(By.tagName("li")); 
     /* 
     for (WebElement we : destinationCities) { 
      System.out.println(we.getText()); 
     } 
     */ 
     destinationCities.get(1).click(); 

     browser.findElement(By.id("departure-date-selector")).click(); 

     WebElement departureMonth = browser.findElement(By.tagName("tbody")); 
     //System.out.println(departureMonth.getText()); 
     List<WebElement> departureDate = departureMonth.findElements(By.tagName("a")); 
     for (WebElement we : departureDate) { 
      System.out.println("~" + we.getText()); 
      we.click(); 
      break; 
     } 

     browser.findElement(By.id("return-date-selector")).click(); 
     Thread.sleep(1000); 
     WebElement returnMonth = browser.findElement(By.tagName("tbody")); 
     System.out.println("<>" + returnMonth.isEnabled()); 
     System.out.println(returnMonth.getText()); 
     List<WebElement> returnDate = returnMonth.findElements(By.tagName("a")); 
     for (WebElement we1 : returnDate) { 
      System.out.println("~" + we1.getText()); 
      we1.click(); 
      break; 
     } 
     Thread.sleep(1000); 
     browser.close(); 
    } 
} 

Wypatruj <tbody> i znaleźć wszystkie widoczne dat przy użyciu znacznika wg nazwy jak „a” będzie można uzyskać wszystkie daty, które są rentowne lub pokazywanie w kolorze zielonym.

Później udoskonal swoją logikę w oparciu o swoje wymagania.

+0

Cześć Jagdeep, ta odpowiedź będzie pomocna dla tych, którzy robią Javę. Przekażę to. Jednak potrzebuję tylko podstawowego polecenia, celu i wartości do umieszczenia w IDE. Podałem powód, dla którego w komentarzu w odpowiedzi na twoje pytanie, dlaczego tak jest. Czy wiesz, jak to zrobić tylko z IDE? – BruceyBandit

+0

Potrzebujesz programowania, aby poznać dostępne daty. IDE jest właśnie do nagrywania, musisz grać z kodem, inaczej trudno będzie uzyskać daty, które są na zielono. Mam nadzieję, że to rozumiesz. IDE ogranicza się do podania formatu tego, jak będzie wyglądać test, pomoże to w szybszym pisaniu kodu. Nie jestem pewien, nawet chłopaki google używać w ten sposób, jak pytasz. Proszę traktować to jako narzędzie do lepszego i szybszego kodowania w automatyzacji, ale nie tylko polegać na IDE. HTH – Jagdeep

+0

Można to zrobić, ponieważ mój kolega to zrobił. Nie miał on pliku z nim, ale stwierdził, że używamy storeEval, whileloops i echo, aby to zrobić. – BruceyBandit

0

Przeprowadzenie tego przez Selenium IDE jest dość trudne. Zobacz link here i zobacz, jak działa gotoIF. Pobierz rozszerzenie użytkownika js z here i dodaj do swojego selen IDE. Jeśli będziesz mógł użyć Selenium Webdriver, będzie to łatwe do osiągnięcia.

Poniżej znajdują się przykłady z Selenium IDE. Podałem tylko przykład, aby wybrać lot wychodzący. W obu przypadkach najpierw wysyłam adres URL z dzisiejszą datą jako datą odlotu i przylotu. Następnie używam gotoIf, aby wybrać pierwszą dostępną datę wychodzącą.

Przykład 1:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head profile="http://selenium-ide.openqa.org/profiles/test-case"> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel="selenium.base" href="http://www.jet2.com/" /> 
<title>21jet2_01</title> 
</head> 
<body> 
<table cellpadding="1" cellspacing="1" border="1"> 
<thead> 
<tr><td rowspan="1" colspan="3">21jet2_01</td></tr> 
</thead><tbody> 
<tr> 
    <td>open</td> 
    <td>/cheap-flights/leeds-bradford/larnaca/2016-02-07/2016-02-07?adults=2</td> 
    <td></td> 
</tr> 
<tr> 
    <td>storeElementPresent</td> 
    <td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, &quot;LS1FlightDay flights&quot;)]/a/div/span[@class='fare']</td> 
    <td>x</td> 
</tr> 
<tr> 
    <td>gotoIf</td> 
    <td>${x} == true</td> 
    <td>select</td> 
</tr> 
<tr> 
    <td>label</td> 
    <td>clickNextMonth</td> 
    <td></td> 
</tr> 
<tr> 
    <td>clickAndWait</td> 
    <td>xpath=//div[@class='monthview outbound ']/div/div[2]/a[@class='changepage nextmonth icon icon-arrow-next']</td> 
    <td></td> 
</tr> 
<tr> 
    <td>storeElementPresent</td> 
    <td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, &quot;LS1FlightDay flights&quot;)]/a/div/span[@class='fare']</td> 
    <td>y</td> 
</tr> 
<tr> 
    <td>gotoIf</td> 
    <td>${y} == false</td> 
    <td>clickNextMonth</td> 
</tr> 
<tr> 
    <td>label</td> 
    <td>select</td> 
    <td></td> 
</tr> 
<tr> 
    <td>click</td> 
    <td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, &quot;LS1FlightDay flights&quot;)]/a/div/span[@class='fare']</td> 
    <td></td> 
</tr> 
</tbody></table> 
</body> 
</html> 

Przykład 2:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head profile="http://selenium-ide.openqa.org/profiles/test-case"> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel="selenium.base" href="http://www.jet2.com/" /> 
<title>21jet2_01</title> 
</head> 
<body> 
<table cellpadding="1" cellspacing="1" border="1"> 
<thead> 
<tr><td rowspan="1" colspan="3">21jet2_01</td></tr> 
</thead><tbody> 
<tr> 
    <td>open</td> 
    <td>/cheap-flights/belfast/alicante/2016-02-07/2016-02-07?adults=2</td> 
    <td></td> 
</tr> 
<tr> 
    <td>storeElementPresent</td> 
    <td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, &quot;LS1FlightDay flights&quot;)]/a/div/span[@class='fare']</td> 
    <td>x</td> 
</tr> 
<tr> 
    <td>gotoIf</td> 
    <td>${x} == true</td> 
    <td>select</td> 
</tr> 
<tr> 
    <td>label</td> 
    <td>clickNextMonth</td> 
    <td></td> 
</tr> 
<tr> 
    <td>clickAndWait</td> 
    <td>xpath=//div[@class='monthview outbound ']/div/div[2]/a[@class='changepage nextmonth icon icon-arrow-next']</td> 
    <td></td> 
</tr> 
<tr> 
    <td>storeElementPresent</td> 
    <td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, &quot;LS1FlightDay flights&quot;)]/a/div/span[@class='fare']</td> 
    <td>y</td> 
</tr> 
<tr> 
    <td>gotoIf</td> 
    <td>${y} == false</td> 
    <td>clickNextMonth</td> 
</tr> 
<tr> 
    <td>label</td> 
    <td>select</td> 
    <td></td> 
</tr> 
<tr> 
    <td>click</td> 
    <td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, &quot;LS1FlightDay flights&quot;)]/a/div/span[@class='fare']</td> 
    <td></td> 
</tr> 
</tbody></table> 
</body> 
</html>