From e4d6d22d522b7657a66ef883ab360109939b9d5b Mon Sep 17 00:00:00 2001 From: rahulpsq Date: Wed, 25 Dec 2024 12:50:16 +0530 Subject: [PATCH 1/3] added fixes --- .github/workflows/maven-workflow-run.yml | 10 --- .../test/java/com/browserstack/LocalTest.java | 77 +++++++++++++------ 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/.github/workflows/maven-workflow-run.yml b/.github/workflows/maven-workflow-run.yml index 1682128..e6b0520 100644 --- a/.github/workflows/maven-workflow-run.yml +++ b/.github/workflows/maven-workflow-run.yml @@ -52,11 +52,6 @@ jobs: with: distribution: 'temurin' java-version: ${{ matrix.java }} - - name: Run mvn test for testng android - run: | - cd android/testng-examples - mvn compile - mvn test - name: Run mvn profile sample-test for testng android run: | cd android/testng-examples @@ -67,11 +62,6 @@ jobs: cd android/testng-examples mvn compile mvn test -P sample-local-test -D"browserstack.app"="./LocalSample.apk" - - name: Run mvn test for testng ios - run: | - cd ios/testng-examples - mvn compile - mvn test - name: Run mvn profile sample-test for testng ios run: | cd ios/testng-examples diff --git a/android/testng-examples/src/test/java/com/browserstack/LocalTest.java b/android/testng-examples/src/test/java/com/browserstack/LocalTest.java index cc4387b..84ff507 100644 --- a/android/testng-examples/src/test/java/com/browserstack/LocalTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/LocalTest.java @@ -5,6 +5,7 @@ import java.util.List; import org.apache.commons.io.FileUtils; +import org.openqa.selenium.*; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebElement; @@ -19,30 +20,60 @@ public class LocalTest extends AppiumTest { @Test public void test() throws Exception { - WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( - ExpectedConditions.elementToBeClickable(AppiumBy.id("com.example.android.basicnetworking:id/test_action"))); - searchElement.click(); - WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( - ExpectedConditions.elementToBeClickable(AppiumBy.className("android.widget.TextView"))); - - WebElement testElement = null; - List allTextViewElements = driver.findElements(AppiumBy.className("android.widget.TextView")); - Thread.sleep(10); - for(WebElement textElement : allTextViewElements) { - if(textElement.getText().contains("The active connection is")) { - testElement = textElement; - } + try { + WebElement searchElement = new WebDriverWait(driver, Duration.ofSeconds(30)) + .until(ExpectedConditions.elementToBeClickable(AppiumBy.id("com.example.android.basicnetworking:id/test_action"))); + searchElement.click(); + + new WebDriverWait(driver, Duration.ofSeconds(30)) + .until(ExpectedConditions.presenceOfElementLocated(AppiumBy.className("android.widget.TextView"))); + + WebElement testElement = findTextViewWithRetry("The active connection is", 3, 2000); + + if (testElement == null) { + takeScreenshot("screenshot.png"); + throw new Error("Cannot find the needed TextView element from app after retries."); + } + + String matchedString = testElement.getText(); + System.out.println(matchedString); + + Assert.assertTrue(matchedString.contains("The active connection is wifi")); + Assert.assertTrue(matchedString.contains("Up and running")); + } catch (Exception e) { + takeScreenshot("error-screenshot.png"); + throw e; + } } - if(testElement == null) { - File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); - FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "screenshot.png")); - System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "screenshot.png"); - throw new Error("Cannot find the needed TextView element from app"); + private WebElement findTextViewWithRetry(String textToMatch, int retries, int waitMillis) throws InterruptedException { + WebElement matchedElement = null; + + for (int i = 0; i < retries; i++) { + List allTextViewElements = driver.findElements(AppiumBy.className("android.widget.TextView")); + + for (WebElement textElement : allTextViewElements) { + if (textElement.getText().contains(textToMatch)) { + matchedElement = textElement; + break; + } + } + if (matchedElement != null) { + break; + } + Thread.sleep(waitMillis); + } + return matchedElement; } - String matchedString = testElement.getText(); - System.out.println(matchedString); - Assert.assertTrue(matchedString.contains("The active connection is wifi")); - Assert.assertTrue(matchedString.contains("Up and running")); + + private void takeScreenshot(String fileName) { + try { + File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); + String filePath = System.getProperty("user.dir") + File.separator + fileName; + FileUtils.copyFile(scrFile, new File(filePath)); + System.out.println("Screenshot stored at " + filePath); + } catch (Exception e) { + System.err.println("Failed to take screenshot: " + e.getMessage()); + } } -} +} \ No newline at end of file From 0c81c7cd876217447c6de752f0f04109d5bbc57e Mon Sep 17 00:00:00 2001 From: rahulpsq Date: Wed, 25 Dec 2024 13:13:08 +0530 Subject: [PATCH 2/3] added fix --- .../src/test/java/com/browserstack/FirstTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/testng-examples/src/test/java/com/browserstack/FirstTest.java b/ios/testng-examples/src/test/java/com/browserstack/FirstTest.java index f7e990e..5c3d57b 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/FirstTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/FirstTest.java @@ -20,6 +20,7 @@ public void test() throws Exception { textButton.click(); WebElement textInput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Input"))); + Thread.sleep(1000); textInput.sendKeys("hello@browserstack.com"+"\n"); Thread.sleep(5000); From 7cbaa7a09e72652605744908daa5aace5ffdd460 Mon Sep 17 00:00:00 2001 From: rahulpsq Date: Wed, 25 Dec 2024 13:58:18 +0530 Subject: [PATCH 3/3] added fix --- .../test/java/com/browserstack/LocalTest.java | 75 ++++++------------- 1 file changed, 22 insertions(+), 53 deletions(-) diff --git a/android/testng-examples/src/test/java/com/browserstack/LocalTest.java b/android/testng-examples/src/test/java/com/browserstack/LocalTest.java index 84ff507..48a7a3b 100644 --- a/android/testng-examples/src/test/java/com/browserstack/LocalTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/LocalTest.java @@ -5,7 +5,6 @@ import java.util.List; import org.apache.commons.io.FileUtils; -import org.openqa.selenium.*; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebElement; @@ -20,60 +19,30 @@ public class LocalTest extends AppiumTest { @Test public void test() throws Exception { - try { - WebElement searchElement = new WebDriverWait(driver, Duration.ofSeconds(30)) - .until(ExpectedConditions.elementToBeClickable(AppiumBy.id("com.example.android.basicnetworking:id/test_action"))); - searchElement.click(); - - new WebDriverWait(driver, Duration.ofSeconds(30)) - .until(ExpectedConditions.presenceOfElementLocated(AppiumBy.className("android.widget.TextView"))); - - WebElement testElement = findTextViewWithRetry("The active connection is", 3, 2000); - - if (testElement == null) { - takeScreenshot("screenshot.png"); - throw new Error("Cannot find the needed TextView element from app after retries."); - } - - String matchedString = testElement.getText(); - System.out.println(matchedString); - - Assert.assertTrue(matchedString.contains("The active connection is wifi")); - Assert.assertTrue(matchedString.contains("Up and running")); - } catch (Exception e) { - takeScreenshot("error-screenshot.png"); - throw e; - } + WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.id("com.example.android.basicnetworking:id/test_action"))); + searchElement.click(); + WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.className("android.widget.TextView"))); + + WebElement testElement = null; + List allTextViewElements = driver.findElements(AppiumBy.xpath("//android.widget.TextView[@resource-id='com.example.android.basicnetworking:id/textView']")); + Thread.sleep(10); + for(WebElement textElement : allTextViewElements) { + if(textElement.getText().contains("The active connection is")) { + testElement = textElement; + } } - private WebElement findTextViewWithRetry(String textToMatch, int retries, int waitMillis) throws InterruptedException { - WebElement matchedElement = null; - - for (int i = 0; i < retries; i++) { - List allTextViewElements = driver.findElements(AppiumBy.className("android.widget.TextView")); - - for (WebElement textElement : allTextViewElements) { - if (textElement.getText().contains(textToMatch)) { - matchedElement = textElement; - break; - } - } - if (matchedElement != null) { - break; - } - Thread.sleep(waitMillis); - } - return matchedElement; + if(testElement == null) { + File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); + FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "screenshot.png")); + System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "screenshot.png"); + throw new Error("Cannot find the needed TextView element from app"); } - - private void takeScreenshot(String fileName) { - try { - File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); - String filePath = System.getProperty("user.dir") + File.separator + fileName; - FileUtils.copyFile(scrFile, new File(filePath)); - System.out.println("Screenshot stored at " + filePath); - } catch (Exception e) { - System.err.println("Failed to take screenshot: " + e.getMessage()); - } + String matchedString = testElement.getText(); + System.out.println(matchedString); + Assert.assertTrue(matchedString.contains("The active connection is wifi")); + Assert.assertTrue(matchedString.contains("Up and running")); } } \ No newline at end of file