|
| 1 | +package com.browserstack.automate; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileNotFoundException; |
| 7 | +import java.util.List; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import com.browserstack.appautomate.AppAutomateClient; |
| 11 | +import com.browserstack.automate.exception.AppAutomateException; |
| 12 | +import com.browserstack.automate.exception.AutomateException; |
| 13 | +import com.browserstack.automate.exception.BuildNotFound; |
| 14 | +import com.browserstack.automate.exception.InvalidFileExtensionException; |
| 15 | +import com.browserstack.automate.exception.SessionNotFound; |
| 16 | +import com.browserstack.automate.model.Build; |
| 17 | +import com.browserstack.automate.model.Session; |
| 18 | +import com.browserstack.client.util.Tools; |
| 19 | + |
| 20 | +public class AppAutomateClientTest { |
| 21 | + |
| 22 | + private String username; |
| 23 | + private String key; |
| 24 | + private AppAutomateClient appAutomateClient; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setup() { |
| 28 | + username = System.getenv("BROWSERSTACK_USER"); |
| 29 | + key = System.getenv("BROWSERSTACK_ACCESSKEY"); |
| 30 | + appAutomateClient = new AppAutomateClient(username, key); |
| 31 | + } |
| 32 | + |
| 33 | + @Test(expected = IllegalArgumentException.class) |
| 34 | + public void testInvalidClientArgs() { |
| 35 | + new AppAutomateClient(null, null); |
| 36 | + } |
| 37 | + |
| 38 | + @Test(expected = FileNotFoundException.class) |
| 39 | + public void testAppUploadInvalidFileLocation() throws FileNotFoundException, AppAutomateException, InvalidFileExtensionException { |
| 40 | + appAutomateClient.uploadApp("some_random_path"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testAppUploadInvalidCredentials() throws FileNotFoundException, InvalidFileExtensionException { |
| 45 | + AppAutomateClient appAutomateClient2 = new AppAutomateClient("invalid_username", "invalid_key"); |
| 46 | + try { |
| 47 | + String absolutePath = new File(getClass().getClassLoader().getResource("dummy.apk").getFile()).getAbsolutePath(); |
| 48 | + appAutomateClient2.uploadApp(absolutePath); |
| 49 | + } catch (AppAutomateException e) { |
| 50 | + assertTrue(e.getMessage().toLowerCase().contains("unauthorized")); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testAppUpload() { |
| 56 | + String appPath = System.getenv("APP_PATH"); |
| 57 | + if(appPath != null) { |
| 58 | + String appId; |
| 59 | + try { |
| 60 | + appId = appAutomateClient.uploadApp(appPath); |
| 61 | + assertTrue("App uploaded successfully :",!Tools.isStringEmpty(appId)); |
| 62 | + } catch (AppAutomateException e) { |
| 63 | + assertTrue("AutomateException ",false); |
| 64 | + } catch (FileNotFoundException e) { |
| 65 | + assertTrue("FileNotFound : ",false); |
| 66 | + } catch (InvalidFileExtensionException e) { |
| 67 | + assertTrue("InvalidExtension : ",false); |
| 68 | + } |
| 69 | + |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testGetSession() { |
| 75 | + try { |
| 76 | + Build build = appAutomateClient.getBuilds().get(0); |
| 77 | + List<Session> sessions = appAutomateClient.getSessions(build.getId()); |
| 78 | + |
| 79 | + Session session1 = sessions.get(0); |
| 80 | + assertTrue("Session", session1 != null); |
| 81 | + assertTrue("Session Id", session1.getId() != null); |
| 82 | + |
| 83 | + Session session2 = appAutomateClient.getSession(session1.getId()); |
| 84 | + assertEquals(session1.getId(), session2.getId()); |
| 85 | + assertEquals(session1.getBrowser(), session2.getBrowser()); |
| 86 | + } catch (BuildNotFound e) { |
| 87 | + assertTrue(false); |
| 88 | + } catch (SessionNotFound e) { |
| 89 | + assertTrue(false); |
| 90 | + } catch (AppAutomateException e) { |
| 91 | + assertTrue(false); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments