Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

crossbrowsertesting/selenium-touch_actions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Getting Started with Selenium TouchActions and CrossBrowserTesting

For this document, we provide all example files in our Selenium TouchActions GitHub Repository.

The TouchActions class of Selenium implements actions for touch enabled devices, reusing the available composite and builder design patterns from Actions.

In this guide we will use JUnit for testing using the Selenium Webdriver and Java programming language to demonstrate using the longpress touch action to draw within a canvas element on iOS.

Running a test

You’ll need to use your Username and Authkey to run your tests on CrossBrowserTesting. To get yours, sign up for a free trial or purchase a plan.
  1. Download Selenium WebDriver for Java
  2. Add the Selenium jars to the build path of your project
    • Right click your project folder and select Properties
    • From the Java Build Path tab, select the Libraries tab
    • Select Add External JARs and add all jars for Selenium
    • Apply and Close
  3. Create a new JUnit Test Case
    • Right click your project folder
    • Hover over New and select JUnit Test Case
    • Name your test case and select Finish
    • Select OK to add JUnit to the build path
  4. Copy the following content:
    import java.net.URL;
    import java.util.concurrent.TimeUnit;
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import com.mashape.unirest.http.Unirest;
    import com.mashape.unirest.http.exceptions.UnirestException;
    import io.appium.java_client.TouchAction;
    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.ios.IOSDriver;
    import io.appium.java_client.ios.IOSElement;
    import io.appium.java_client.touch.LongPressOptions;
    import io.appium.java_client.touch.offset.PointOption;
    

    ​ ​ class TouchActionExample{ String username = "YOUR_USERNAME"; //username can be found on the Manage Account page, ensure @ is encoded with %40 String authkey = "YOUR_AUTHKEY"; //authkey can be found on the Manage Account page String score;

    AppiumDriver driver;
    String sessionid;
    

    ​ @BeforeEach void setUp() throws Exception {

         //set capabilites 
         DesiredCapabilities caps = new DesiredCapabilities();  															//additional capabilites can be found at https://app.crossbrowsertesting.com/selenium/run   
        	     
         caps.setCapability("browserName", "Safari");
         caps.setCapability("deviceName", "iPhone 11 Pro Max");
         caps.setCapability("platformVersion", "13.2");
         caps.setCapability("platformName", "iOS");
         caps.setCapability("deviceOrientation", "portrait");
         caps.setCapability("record_video", true);   
    

    ​ String hubAddress = String.format("http://%s:%[email protected]:80/wd/hub", username, authkey); URL url = new URL(hubAddress); ​ driver = new IOSDriver(url, caps); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); sessionid = driver.getSessionId().toString();

    }
    

    ​ @AfterEach void tearDown() throws Exception { driver.quit(); //close the driver } ​ @Test void test() { try { driver.get("https://sketchtoy.com/"); Thread.sleep(5000);

    		 IOSElement element = driver.findElement(By.xpath("//div[@class='sketch-canvas-container']/canvas"));
    
    		 
    		 new TouchAction(driver)
    		 .longPress(LongPressOptions.longPressOptions().withPosition(PointOption.point(184, 233)))
    		 .moveTo(PointOption.point(200, 200))
    		 .moveTo(PointOption.point(250, 250))
    		 .release()
    		 .perform();
    		
    	     Thread.sleep(5000);
    		
    	
    		score = "pass";																												//set score to pass
    		setScore();
    	}
    	catch (Exception e){
    		score = "fail";																												//set score to fail if any exceptions
    		try{
    			setScore();
    		}
    		catch(Exception err) {
    			System.out.println(err);
    		}
    		System.out.println(e);
    		
    	}
    

    }
    //setScore function - calls our api and set the score to pass or fail after a test
    public void setScore() throws UnirestException{
    	Unirest.put("https://crossbrowsertesting.com/api/v3/selenium/"+sessionid)
    			.basicAuth(username, authkey)
    			.field("action","set_score")
    			.field("score", score)
    			.asJson();
    	
    }
    //takeSnapshot function - calls our api and takes snapshots throughout your automated test
    public void takeSnapshot(String seleniumTestId) throws UnirestException {
        Unirest.post("http://crossbrowsertesting.com/api/v3/selenium/{seleniumTestId}/snapshots")
                .basicAuth(username, authkey)
                .routeParam("seleniumTestId", seleniumTestId)
                .asJson();
    

    ​ } ​ }

    Run your test by selecting the Run button

Congratulations! You have successfully created a test using Selenium TouchActions with CBT and JUnit. Now you are ready to see your build start to run in the Crossbrowsertesting app.

Conclusions

By following the steps outlined in this guide, you are now able to seamlessly run a test using Selenium TouchActions with CrossBrowserTesting. If you have any questions or concerns, please feel free to reach out to our support team.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages