View Javadoc

1   package fit;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.io.PrintStream;
6   import java.net.BindException;
7   import java.net.ServerSocket;
8   
9   import org.openqa.selenium.server.SeleniumServer;
10  
11  import com.thoughtworks.selenium.DefaultSelenium;
12  import com.thoughtworks.selenium.Selenium;
13  import com.thoughtworks.selenium.Wait;
14  
15  public class ContextSeleniumFixture extends ArgumentFixture
16  {
17    private static final int SELENIUM_PORT = 4444;
18  
19    private static PrintStream ps;
20  
21    private static SeleniumServer server;
22  
23    public int defaultTimeout = 3000;
24  
25    public Selenium selenium;
26  
27    @Override
28    protected void interpretTables(Parse tables)
29    {
30      super.interpretTables(tables);
31    }
32  
33    @Override
34    public void doCells(Parse cells)
35    {
36      if (cells.text().equals("start") || selenium != null)
37      {
38        super.doCells(cells);
39      }
40      else
41      {
42        wrong(cells, "Selenium Client is not init!");
43      }
44    }
45  
46    public void timeout(Argument selektor, Argument arg)
47    {
48      String timeoutValue = selektor.text();
49      this.defaultTimeout = Integer.parseInt(timeoutValue);
50    }
51  
52    static
53    {
54      startSeleniumServer();
55    }
56  
57    public void start(Argument selektor, Argument arg)
58    {
59      String testhost = selektor.text();
60      String port = arg.text();
61      String browser = arg.getCell().more.text();
62      String baseUrl = arg.getCell().more.more.text();
63  
64      if (!connectSelenium(selektor, testhost, port, browser, baseUrl, true))
65      {
66        System.out.println("Unable to find SELENIUM !!! ");
67        setStop(true);
68      }
69    }
70  
71    public static void startSeleniumServer()
72    {
73      try
74      {
75        ServerSocket serverSocket = new ServerSocket(SELENIUM_PORT);
76        serverSocket.close();
77  
78        try
79        {
80          // SeleniumServer.main(new String[] {"-multiWindow"});
81          server = new SeleniumServer(4444, true, true);
82          server.start();
83        }
84        catch (Exception e)
85        {
86          System.err.println("Could not create Selenium Server because of: "
87              + e.getMessage());
88          e.printStackTrace();
89        }
90      }
91      catch (BindException e)
92      {
93        System.out.println("Selenium server already up, will reuse...");
94      }
95      catch (IOException e)
96      {
97        System.out.println("Selenium server already up, will reuse...");
98        e.printStackTrace();
99      }
100 
101   }
102 
103   protected boolean connectSelenium(Argument selektor, String testhost,
104       String port, String browser, String baseUrl, boolean markSelector)
105   {
106     selenium = new DefaultSelenium(testhost, Integer.valueOf(port), browser,
107         baseUrl);
108 
109     try
110     {
111       selenium.start();
112       return true;
113     }
114     catch (Exception e)
115     {
116       if (markSelector)
117       {
118         exception(selektor, e);
119       }
120       return false;
121     }
122   }
123 
124   public void open(Argument selektor, Argument arg) throws Exception
125   {
126     String url = selektor.text();
127     try
128     {
129       selenium.open(url);
130     }
131     catch (Exception e)
132     {
133       exception(selektor, e);
134       return;
135     }
136   }
137 
138   public void stopSelenium(Argument selektor, Argument arg)
139   {
140     selenium.stop();
141   }
142 
143   public void type(Argument selektor, Argument arg) throws Exception
144   {
145     try
146     {
147       selenium.type(selektor.text(), arg.text());
148     }
149     catch (Exception e)
150     {
151       exception(selektor, e);
152       return;
153     }
154   }
155 
156   public void clickAndWait(Argument selektor, Argument arg) throws Exception
157   {
158     try
159     {
160       selenium.click(selektor.text());
161 
162       selenium.waitForPageToLoad(getTimeout(arg.getCell()));
163     }
164     catch (Exception e)
165     {
166       exception(selektor, e);
167       return;
168     }
169   }
170 
171   private String getTimeout(String text)
172   {
173     if (text != null && text.length() > 0)
174     {
175       return text;
176     }
177     return Integer.toString(defaultTimeout);
178   }
179 
180   private String getTimeout(Parse cell)
181   {
182     if (cell == null)
183     {
184       return getTimeout("");
185     }
186     return getTimeout(cell.text());
187   }
188 
189   private Integer getTimeoutInt(String text)
190   {
191     return Integer.parseInt(getTimeout(text));
192   }
193 
194   private int getTimeoutInt(Parse cell)
195   {
196     if (cell == null)
197     {
198       return getTimeoutInt("");
199     }
200     return getTimeoutInt(cell.text());
201   }
202 
203   public void click(Argument selektor, Argument arg) throws Exception
204   {
205     try
206     {
207       selenium.click(selektor.text());
208     }
209     catch (Exception e)
210     {
211       exception(selektor, e);
212       return;
213     }
214   }
215   
216   public void uncheck(Argument selektor, Argument arg) throws Exception
217   {
218     try
219     {
220       selenium.uncheck(selektor.text());
221     }
222     catch (Exception e)
223     {
224       exception(selektor, e);
225       return;
226     }
227   }
228   
229   public void check(Argument selektor, Argument arg) throws Exception
230   {
231     try
232     {
233       selenium.check(selektor.text());
234     }
235     catch (Exception e)
236     {
237       exception(selektor, e);
238       return;
239     }
240   }
241 
242   public void mouseMove(Argument selektor, Argument arg) throws Exception
243   {
244     try
245     {
246       selenium.mouseMove(selektor.text());
247     }
248     catch (Exception e)
249     {
250       exception(selektor, e);
251       return;
252     }
253   }
254 
255   public void mouseOver(Argument selektor, Argument arg) throws Exception
256   {
257     try
258     {
259       selenium.mouseOver(selektor.text());
260     }
261     catch (Exception e)
262     {
263       exception(selektor, e);
264       return;
265     }
266   }
267 
268   public void mouseOut(Argument selektor, Argument arg) throws Exception
269   {
270     try
271     {
272       selenium.mouseOut(selektor.text());
273     }
274     catch (Exception e)
275     {
276       exception(selektor, e);
277       return;
278     }
279   }
280 
281   public void mouseUp(Argument selektor, Argument arg) throws Exception
282   {
283     try
284     {
285       selenium.mouseUp(selektor.text());
286     }
287     catch (Exception e)
288     {
289       exception(selektor, e);
290       return;
291     }
292   }
293 
294   public void verifyTextNotPresent(Argument selektor, Argument arg)
295   {
296     String text = selektor.text();
297     if (!selenium.isTextPresent(text))
298     {
299       right(selektor);
300     }
301     else
302     {
303       wrong(selektor.getCell(), text + " is present on site");
304     }
305   }
306 
307   public void verifyTextPresent(Argument selektor, Argument arg)
308   {
309     String text = selektor.text();
310     if (selenium.isTextPresent(text))
311     {
312       right(selektor);
313     }
314     else
315     {
316       dumpWrongImage("verifyTextPresent");
317       wrong(selektor.getCell(), text + " is not present on site");
318     }
319   }
320 
321   private void dumpWrongImage(String name)
322   {
323     String curDir = System.getProperty("user.dir");
324     File f = new File(curDir, name + ".png");
325     // selenium.captureEntirePageScreenshot(f.getPath());
326   }
327 
328   public void verifyTitle(Argument selektor, Argument arg)
329   {
330     try
331     {
332       String actualText = selenium.getTitle();
333       if (actualText.equals(selektor.text()))
334       {
335         right(selektor);
336       }
337       else
338       {
339         wrong(selektor, actualText);
340       }
341     }
342     catch (Exception e)
343     {
344       exception(selektor, e);
345       return;
346     }
347   }
348 
349   public void verifyTable(Argument selektor, Argument arg)
350   {
351     try
352     {
353       String actualText = selenium.getTable(selektor.text());
354       if (actualText.equals(arg.text()))
355       {
356         right(arg);
357       }
358       else
359       {
360         wrong(selektor, actualText);
361       }
362     }
363     catch (Exception e)
364     {
365       exception(selektor, e);
366       return;
367     }
368   }
369 
370   public void verifyValue(Argument selektor, Argument arg)
371   {
372     try
373     {
374       String actualText = selenium.getValue(selektor.text());
375       if (actualText.equals(arg.text()))
376       {
377         right(arg);
378       }
379       else
380       {
381         wrong(selektor, actualText);
382       }
383     }
384     catch (Exception e)
385     {
386       exception(selektor, e);
387       return;
388     }
389   }
390 
391   public void verifyVisible(Argument selektor, Argument arg)
392   {
393     try
394     {
395 
396       if (selenium.isVisible(selektor.text()))
397       {
398         right(selektor);
399       }
400       else
401       {
402         wrong(selektor);
403       }
404     }
405     catch (Exception e)
406     {
407       exception(selektor, e);
408       return;
409     }
410   }
411 
412   public void verifyChecked(Argument selektor, Argument arg)
413   {
414     try
415     {
416       if (selenium.isChecked(selektor.text()))
417       {
418         right(selektor);
419       }
420       else
421       {
422         wrong(selektor);
423       }
424     }
425     catch (Exception e)
426     {
427       exception(selektor, e);
428       return;
429     }
430   }
431 
432   public void verifyEditable(Argument selektor, Argument arg)
433   {
434     try
435     {
436       if (selenium.isEditable(selektor.text()))
437       {
438         right(selektor);
439       }
440       else
441       {
442         wrong(selektor);
443       }
444     }
445     catch (Exception e)
446     {
447       exception(selektor, e);
448       return;
449     }
450   }
451 
452   public void verifyExpression(Argument selektor, Argument arg)
453   {
454     try
455     {
456       if (selenium.getExpression(selektor.text()).equals(arg.text()))
457       {
458         right(selektor);
459         right(arg);
460       }
461       else
462       {
463         wrong(selektor);
464         wrong(arg);
465       }
466     }
467     catch (Exception e)
468     {
469       exception(selektor, e);
470       return;
471     }
472   }
473 
474   public void verifyConfirmation(Argument selektor, Argument arg)
475   {
476     try
477     {
478       if (selenium.getConfirmation().equals(selektor.text()))
479       {
480         right(selektor);
481       }
482       else
483       {
484         wrong(selektor);
485       }
486     }
487     catch (Exception e)
488     {
489       exception(selektor, e);
490       return;
491     }
492   }
493 
494   public void verifySelectedLable(Argument selektor, Argument arg)
495   {
496     try
497     {
498       if ((selenium.getSelectedLabel(selektor.text()).equals(arg.text())))
499       {
500         right(selektor);
501         right(arg);
502       }
503       else
504       {
505         wrong(selektor);
506         wrong(arg, selenium.getSelectedLabel(selektor.text()));
507       }
508 
509     }
510     catch (Exception e)
511     {
512       exception(selektor, e);
513       return;
514     }
515   }
516 
517   public void verifyText(Argument selektor, Argument arg)
518   {
519     try
520     {
521       String actualText = selenium.getText(selektor.text());
522       if (actualText.equals(arg.text()))
523       {
524         right(arg);
525       }
526       else
527       {
528         wrong(selektor, actualText);
529       }
530     }
531     catch (Exception e)
532     {
533       exception(selektor, e);
534       return;
535     }
536   }
537 
538   public void verifyElementPresent(Argument selektor, Argument arg)
539   {
540     String text = selektor.text();
541     if (selenium.isElementPresent(text))
542     {
543       right(selektor);
544     }
545     else
546     {
547       wrong(selektor.getCell(), text + " is not present on site");
548     }
549   }
550 
551   public void verifyPromptPresent(Argument selektor, Argument arg)
552   {
553     if (selenium.isPromptPresent())
554     {
555       right(selektor);
556     }
557     else
558     {
559       wrong(selektor.getCell(), "No prompt present on site");
560     }
561   }
562 
563   public void verifyAlertPresent(Argument selektor, Argument arg)
564   {
565     if (selenium.isAlertPresent())
566     {
567       right(selektor);
568     }
569     else
570     {
571       wrong(selektor.getCell(), " No Alert present on site");
572     }
573   }
574 
575   // public void verifyCookiePresent(Argument selektor, Argument arg)
576   // {
577   // String text = selektor.text();
578   // if (selenium.isCookiePresent(text))
579   // {
580   // right(selektor);
581   // }
582   // else
583   // {
584   // wrong(selektor.getCell(), text + " is not present on site");
585   // }
586   // }
587 
588   public void assertAlert(Argument selektor, Argument arg)
589   {
590     verifyAlertPresent(selektor, arg);
591     if (!selektor.isRight())
592     {
593       setStop(true);
594     }
595   }
596 
597   public void assertAlertNotPresent(Argument selektor, Argument arg)
598   {
599     verifyAlertPresent(selektor, arg);
600     if (selektor.isRight())
601     {
602       setStop(true);
603     }
604   }
605 
606   // public void assertAllButtons(Argument selektor, Argument arg)
607   // {
608   // selenium.getAllButtons();
609   // }
610   //
611   // public void assertAllFields(Argument selektor, Argument arg)
612   // {
613   // selenium.getAllFields();
614   // }
615   //
616   // public void assertAllWindowIds(Argument selektor, Argument arg)
617   // {
618   // selenium.getAllWindowIds();
619   // }
620   //
621   // public void assertAllWindowTitles(Argument selektor, Argument
622   // arg)
623   // {
624   // selenium.getAllWindowNames();
625   // }
626   //
627   // public void assertAttributeFromAllWindows(Argument selektor,
628   // Argument
629   // arg)
630   // {
631   // selenium.getAttributeFromAllWindows(selektor.text());
632   // }
633   //
634   // public void assertAllWindowNames(Argument selektor, Argument arg)
635   // {
636   // selenium.getAllWindowNames();
637   // }
638   //
639   // public void assertBodyText(Argument selektor, Argument arg)
640   // {
641   // selenium.getBodyText();
642   // }
643 
644   public void assertChecked(Argument selektor, Argument arg)
645   {
646     verifyChecked(selektor, arg);
647     if (!selektor.isRight())
648     {
649       setStop(true);
650     }
651   }
652 
653   public void assertExpression(Argument selektor, Argument arg)
654   {
655     verifyExpression(selektor, arg);
656     if (!selektor.isRight() || !arg.isRight())
657     {
658       setStop(true);
659     }
660   }
661 
662   // public void assertConfirmation(Argument selektor, Argument arg)
663   // {
664   // // TODO
665   // }
666   //
667   // public void assertEval(Argument selektor, Argument arg)
668   // {
669   // // TODO
670   // }
671 
672   public void waitForTextPresent(Argument selektor, Argument arg)
673   {
674     Wait x = new WaitForTextToAppear(selektor.text());
675     x.wait("Cannot find text " + selektor.text() + " after " + defaultTimeout
676         + " seconds", defaultTimeout * 1000);
677   }
678 
679   public void waitForPageLoad(Argument selektor, Argument arg)
680   {
681     String wartezeit = arg.getCell().text();
682     if (wartezeit == null || wartezeit.length() == 0)
683     {
684       selenium.waitForPageToLoad(Integer.toString(defaultTimeout));
685     }
686     else
687     {
688       selenium.waitForPageToLoad(wartezeit);
689     }
690   }
691 
692   public void waitForText(Argument selektor, Argument arg)
693   {
694     Wait x = new WaitForTextToAppear(arg.text());
695     x.wait("Cannot find text " + arg.text() + " after " + defaultTimeout
696         + " seconds", defaultTimeout * 1000);
697   }
698 
699   public void waitForChecked(Argument selektor, Argument arg)
700   {
701     String locator = selektor.text();
702     Boolean checked = arg.text().matches("true");
703     Wait x = new WaitForChecked(locator, checked);
704     try
705     {
706       x.wait("Checkbox " + locator + " is " + (checked ? "not" : "still")
707           + " checked after " + defaultTimeout + " seconds",
708           defaultTimeout * 1000);
709     }
710     catch (Exception e)
711     {
712       exception(selektor, e);
713       return;
714     }
715   }
716 
717   public void waitForElementPresent(Argument selektor, Argument arg)
718   {
719     Wait x = new WaitForElementToAppear(selektor.text());
720     try
721     {
722       int timeout = getTimeoutInt(arg.getCell());
723       x.wait("Cannot find element " + selektor.text() + " after " + timeout
724           + " ms", timeout);
725     }
726     catch (Exception e)
727     {
728       exception(selektor, e);
729       return;
730     }
731   }
732 
733   public void waitForFieldValue(Argument selektor, Argument arg)
734   {
735     Wait x = new WaitForFieldValue(selektor.text(), arg.text());
736     try
737     {
738       x.wait("Cannot find value" + arg.text() + " in " + selektor.text()
739           + " after " + defaultTimeout + " seconds", defaultTimeout * 1000);
740     }
741     catch (Exception e)
742     {
743       exception(selektor, e);
744       return;
745     }
746   }
747 
748   public void assertText(Argument selektor, Argument arg)
749   {
750     verifyText(selektor, arg);
751     if (!selektor.isRight() || !arg.isRight())
752     {
753       setStop(true);
754     }
755   }
756 
757   public void assertElementPresent(Argument selektor, Argument arg)
758   {
759     verifyElementPresent(selektor, arg);
760     if (!selektor.isRight())
761     {
762       setStop(true);
763     }
764   }
765 
766   public void assertTextPresent(Argument selektor, Argument arg)
767   {
768     verifyTextPresent(selektor, arg);
769     if (!selektor.isRight())
770     {
771       setStop(true);
772     }
773   }
774 
775   public void assertAlertPresent(Argument selektor, Argument arg)
776   {
777     verifyAlertPresent(selektor, arg);
778     if (!selektor.isRight())
779     {
780       setStop(true);
781     }
782   }
783 
784   private class WaitForTextToAppear extends Wait
785   {
786     private String text;
787 
788     public WaitForTextToAppear(String text)
789     {
790       this.text = text;
791     }
792 
793     public boolean until()
794     {
795       return selenium.isTextPresent(text);
796     }
797   }
798 
799   private class WaitForChecked extends Wait
800   {
801     private String locator;
802 
803     private boolean checked;
804 
805     public WaitForChecked(String locator, boolean checked)
806     {
807       this.locator = locator;
808       this.checked = checked;
809     }
810 
811     public boolean until()
812     {
813       if (!selenium.isElementPresent(locator))
814         return false;
815       return selenium.isChecked(locator) == checked;
816     }
817   }
818 
819   private class WaitForElementToAppear extends Wait
820   {
821     private String text;
822 
823     public WaitForElementToAppear(String text)
824     {
825       this.text = text;
826     }
827 
828     public boolean until()
829     {
830       return selenium.isElementPresent(text);
831     }
832   }
833 
834   @SuppressWarnings("unused")
835   private class WaitForElementToDisappear extends Wait
836   {
837     private String text;
838 
839     public WaitForElementToDisappear(String text)
840     {
841       this.text = text;
842     }
843 
844     public boolean until()
845     {
846       return (!selenium.isElementPresent(text));
847     }
848   }
849 
850   private class WaitForFieldValue extends Wait
851   {
852     private String text;
853 
854     private String elementLocator;
855 
856     public WaitForFieldValue(String element, String value)
857     {
858       this.text = value;
859       this.elementLocator = element;
860     }
861 
862     public boolean until()
863     {
864       String value = selenium.getValue(elementLocator);
865       if (value == null)
866       {
867         return false;
868       }
869       return value.indexOf(text) >= 0;
870     }
871   }
872 
873   public void pause(int milliseconds)
874   {
875     try
876     {
877       Thread.sleep(milliseconds);
878     }
879     catch (InterruptedException iex)
880     {
881       iex.getMessage();
882     }
883   }
884   
885   public void select(Argument selektor, Argument arg)
886   {
887 	  selenium.select(selektor.text(), arg.text());
888   }
889   
890   public void selectAndWait(Argument selektor, Argument arg) throws InterruptedException
891   {
892       select(selektor, arg);
893 	  Thread.sleep(2000);
894   }
895   
896 }