If you ever struggeled to understand how to create constructors for extended classes and just constructors in general I found a very usful post here
Accepting SSL popups in web mobile testing with Appium
It’s been ages since my last post but as I returned from maternity leave and got back to test automation with Selenium on desktop and Appium on mobile here is my first post….
Today I came across very annoying issue when running my appium tests in mobile browser – SSL popup (Cannot verify Server Identity) which doesn’t allow to proceed with the test step.
To resolve it, all you need to do is add capability to the Appium driver:
capabilities.setCapability(“autoAcceptAlerts”, “true”);
This will accept SSL certificate automatically and you test can proceed to the next step.
Enjoy!
Eclipse and Ant encoding problem – UTF-8 / Cyrillic
Recently I’ve encountered a problem while testing some Russian text on one of the pages. The test was very simple:
public static final String PRIVACY_POLICY = "Kонфиденциальность"; private void assertFooterElements() { assertTrue("'Privacy Policy' link is not visible or incorrect", selenium.isTextPresent(PRIVACY_POLICY));
When I run this in Eclipse it was no problem because Eclipse set up encoding properties automatically. When I tried to run it with Ant, I got an error with some weirdly displayed symbols instead of cyrillic characters:
The header is incorrect - the expected header is –ü—Å–∏—Ö–æ–ª–æ–≥–∏—á–µ—Å–∫–∏–π —Ç–µ—Å—Ç
After some investigation I found an easy solution. In my Ant build.xml file I’ve added this line:
encoding="UTF-8"
So it looks like this:
<target name="compile" description="compile the source" depends="init"> <javac srcdir="${src.dir}" destdir="${build.dir}" debug="true" deprecation="true" encoding="UTF-8" </javac> </target>
Worked perfectly fine!
assertThat – Alternative to assertNotEquals that doesn’t exist in JUnit
JUnit doesn’t provide assertNotEquals method but the solution can be found with assertThat method.
assertThat(expectedObject, is(not(actualObject)));
Requires import of libruary: org.hamcrest.CoreMatchers.*
Example:
Web page with dynamic title.
Test: Check that web page title is not “Error page”
public void pageTitleNotError() { assertThat("Error page", is( not(selenium.getTitle ); }
Create Selenium Tests project structure in Eclipse with JUnit
1. Click File -> New -> Java Project
2. Enter Project name and click Finish -> New project will be created in Package Explorer
3. Right-click on a project name and select Build Path -> New source folder
4. Create source folder called src/main/java This is where main code will be stored
5. Create another source folder called scr/test/java This is where tests code will be stored
6. Right-click on a folder called src, select Build path -> Remove from build path
7. Then add packages to src/main/java and src/test/java folders: Right-click on a source folder and select Add-> Package
8. Enter package name which should be in Sun format e.g: com.automation.seleniumutils read more here: http://www.oracle.com/technetwork/java/codeconventions-135099.html It will actually create folders com -> automation ->seleniumutils on your computer. In this case seleniumutils is a folder that contains utilities files for selenium tests, for example SeleniumManager.java class which handles starting and stopping selenium server.
9. Similar to above com.automation.tests package contains actual tests files, e.g. Test1.java
One last step you need to add selenium and JUunit libraries:
1. Right-click on a project name and select Properties
2. In the opened window select Libraries
3. Click Add External Jars and add the following libraries: junit-4.9b2.jar selenium-java-client-driver.jar selenium-server.jar
Selenium Junit – checking a string in the page source
String pageSource = selenium.getHtmlSource().toLowerCase(); assertTrue("error message",pageSource.contains("abcdefg.toLowerCase()));
Selenium jUnit – click random element on the page example
public void selectRandomImage () { int count = selenium.getXpathCount("//div[@id='options']/ul/li").intValue(); //counting how many elements we have under provided xpath //and assigning this number to the "count" variable Random generator = new Random (); //creating random number generator int imageIndex = generator.nextInt(count); //generating random number from 0 to "count" (0-inclusive, count-exclusive) //and assigning to imageIndex variable System.out.println("imageIndex="+imageIndex); //printing out generated random number - just for visibility selenium.click("//div[@id='options']/ul/li["+(imageIndex+1)+"]/button"); //clicking on selected element //(note: 1 needs to be added as xpath elements start from [1]) selenium.waitForPageToLoad("30000"); //waiting for next page to load