From 384d889d0cabce511d9d18fdc4c520240e7010e3 Mon Sep 17 00:00:00 2001 From: Malcolm Shen Date: Thu, 7 Apr 2016 17:26:32 +0800 Subject: [PATCH 1/5] Enhancement: add FairTestRetryAnalyzer to support retries per test level and max retries at global level. --- .../core/FairTestRetryAnalyzer.java | 85 +++++++++++++++++++ .../core/FairTestRetryListener.java | 33 +++++++ .../core/ITestRetryAnalyzer.java | 10 +++ .../seleniumtests/core/TestRetryAnalyzer.java | 5 +- .../reporter/SeleniumTestsReporter.java | 6 +- 5 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/seleniumtests/core/FairTestRetryAnalyzer.java create mode 100644 src/main/java/com/seleniumtests/core/FairTestRetryListener.java create mode 100644 src/main/java/com/seleniumtests/core/ITestRetryAnalyzer.java diff --git a/src/main/java/com/seleniumtests/core/FairTestRetryAnalyzer.java b/src/main/java/com/seleniumtests/core/FairTestRetryAnalyzer.java new file mode 100644 index 0000000..cd551c7 --- /dev/null +++ b/src/main/java/com/seleniumtests/core/FairTestRetryAnalyzer.java @@ -0,0 +1,85 @@ +/* + * Copyright 2015 www.seleniumtests.com + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.seleniumtests.core; + +import org.testng.IRetryAnalyzer; +import org.testng.ITestResult; + +public class FairTestRetryAnalyzer implements IRetryAnalyzer, ITestRetryAnalyzer { + + public static final String TEST_RETRY_COUNT = "fairTestRetryCount"; + public static final String TEST_RETRY_MAX = "fairTestRetryMax"; + private int count = 1; + private int maxCount = 1; + + private int countLimit = 1; + private int maxCountLimit = 2; + + + public FairTestRetryAnalyzer() { + String retryCount = System.getProperty(TEST_RETRY_COUNT); + if (retryCount != null) { + countLimit = Integer.parseInt(retryCount); + } + + String retryMaxCount = System.getProperty(TEST_RETRY_MAX); + if (retryMaxCount != null) { + maxCountLimit = Integer.parseInt(retryMaxCount); + } + } + + public int getCount() { + return this.count; + } + + public int getMaxCount() { + return this.maxCount; + } + + public synchronized boolean retry(final ITestResult result) { + String testClassName = String.format("%s.%s", result.getMethod().getRealClass().toString(), + result.getMethod().getMethodName()); + + Integer retryCount = Integer.class.cast(result.getTestContext().getAttribute("fairTestRetryCount")); + if (retryCount == null) { + count = 1; + } else { + count = retryCount; + } + + if (count <= countLimit && maxCount <= maxCountLimit) { + + result.setAttribute("RETRY", new Integer(count)); + TestLogging.log("[RETRYING] " + testClassName + " FAILED, " + "Retrying " + count + " time", true); + result.getTestContext().setAttribute("fairTestRetryCount", ++count); + + maxCount++; + return true; + } + + return false; + } + + + public boolean retryPeek(final ITestResult result) { + Integer retryCount = Integer.class.cast(result.getTestContext().getAttribute("fairTestRetryCount")); + if (retryCount == null) { + count = 1; + } else { + count = retryCount; + } + + return count <= countLimit && maxCount <= maxCountLimit; + } +} diff --git a/src/main/java/com/seleniumtests/core/FairTestRetryListener.java b/src/main/java/com/seleniumtests/core/FairTestRetryListener.java new file mode 100644 index 0000000..550b54a --- /dev/null +++ b/src/main/java/com/seleniumtests/core/FairTestRetryListener.java @@ -0,0 +1,33 @@ +/* + * Copyright 2015 www.seleniumtests.com + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.seleniumtests.core; + +import org.testng.IAnnotationTransformer; +import org.testng.IRetryAnalyzer; +import org.testng.annotations.ITestAnnotation; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +public class FairTestRetryListener implements IAnnotationTransformer { + + public void transform(final ITestAnnotation annotation, final Class testClass, final Constructor testConstructor, + final Method testMethod) { + IRetryAnalyzer retryAnalyzer = annotation.getRetryAnalyzer(); + if (retryAnalyzer == null) { + annotation.setRetryAnalyzer(FairTestRetryAnalyzer.class); + } + } + +} diff --git a/src/main/java/com/seleniumtests/core/ITestRetryAnalyzer.java b/src/main/java/com/seleniumtests/core/ITestRetryAnalyzer.java new file mode 100644 index 0000000..2670e00 --- /dev/null +++ b/src/main/java/com/seleniumtests/core/ITestRetryAnalyzer.java @@ -0,0 +1,10 @@ +package com.seleniumtests.core; + +import org.testng.ITestResult; + + +public interface ITestRetryAnalyzer { + boolean retryPeek(ITestResult var1); + + int getCount(); +} diff --git a/src/main/java/com/seleniumtests/core/TestRetryAnalyzer.java b/src/main/java/com/seleniumtests/core/TestRetryAnalyzer.java index 865b9eb..ccad0c9 100644 --- a/src/main/java/com/seleniumtests/core/TestRetryAnalyzer.java +++ b/src/main/java/com/seleniumtests/core/TestRetryAnalyzer.java @@ -16,7 +16,7 @@ import org.testng.IRetryAnalyzer; import org.testng.ITestResult; -public class TestRetryAnalyzer implements IRetryAnalyzer { +public class TestRetryAnalyzer implements IRetryAnalyzer, ITestRetryAnalyzer { private static final String TEST_RETRY_COUNT = "testRetryCount"; private int count = 1; @@ -57,4 +57,7 @@ public synchronized boolean retry(final ITestResult result) { return false; } + public boolean retryPeek(final ITestResult result) { + return count <= maxCount; + } } diff --git a/src/main/java/com/seleniumtests/reporter/SeleniumTestsReporter.java b/src/main/java/com/seleniumtests/reporter/SeleniumTestsReporter.java index 23396fe..7a123ee 100644 --- a/src/main/java/com/seleniumtests/reporter/SeleniumTestsReporter.java +++ b/src/main/java/com/seleniumtests/reporter/SeleniumTestsReporter.java @@ -81,7 +81,7 @@ import com.seleniumtests.core.SeleniumTestsContextManager; import com.seleniumtests.core.SeleniumTestsPageListener; import com.seleniumtests.core.TestLogging; -import com.seleniumtests.core.TestRetryAnalyzer; +import com.seleniumtests.core.ITestRetryAnalyzer; import com.seleniumtests.driver.ScreenShot; import com.seleniumtests.driver.ScreenshotUtil; @@ -1113,9 +1113,9 @@ public void onTestFailedButWithinSuccessPercentage(final ITestResult arg0) { } public synchronized void onTestFailure(final ITestResult arg0) { if (arg0.getMethod().getRetryAnalyzer() != null) { - TestRetryAnalyzer testRetryAnalyzer = (TestRetryAnalyzer) arg0.getMethod().getRetryAnalyzer(); + ITestRetryAnalyzer testRetryAnalyzer = (ITestRetryAnalyzer) arg0.getMethod().getRetryAnalyzer(); - if (testRetryAnalyzer.getCount() <= testRetryAnalyzer.getMaxCount()) { + if (testRetryAnalyzer.retryPeek(arg0)) { arg0.setStatus(ITestResult.SKIP); Reporter.setCurrentTestResult(null); } else { From 36758dd839315e2a3b29c09d3ab2c1ddb250374c Mon Sep 17 00:00:00 2001 From: Malcolm Shen Date: Tue, 12 Jul 2016 13:24:07 +0800 Subject: [PATCH 2/5] Fixed WebDriverException(error message: Not implemented yet) in Appium android testing --- .../seleniumtests/browserfactory/AndroidDriverFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/seleniumtests/browserfactory/AndroidDriverFactory.java b/src/main/java/com/seleniumtests/browserfactory/AndroidDriverFactory.java index 37522e0..3bc1ea1 100644 --- a/src/main/java/com/seleniumtests/browserfactory/AndroidDriverFactory.java +++ b/src/main/java/com/seleniumtests/browserfactory/AndroidDriverFactory.java @@ -26,6 +26,7 @@ import com.seleniumtests.driver.DriverConfig; import io.appium.java_client.android.AndroidDriver; +import org.openqa.selenium.WebDriverException; /** * AndroidDriverFactory. @@ -60,8 +61,7 @@ public WebDriver createWebDriver() throws IOException { protected void setPageLoadTimeout(final long timeout) { try { driver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS); - } catch (UnsupportedCommandException e) { - // chromedriver does not support pageLoadTimeout + } catch (WebDriverException e) { } } From a195419ba51a05483ba4ee25482fef47482ec641 Mon Sep 17 00:00:00 2001 From: Malcolm Shen Date: Tue, 12 Jul 2016 13:47:35 +0800 Subject: [PATCH 3/5] Fixed WebDriverException(error message: Not implemented yet) in Appium android testing --- .../com/seleniumtests/browserfactory/AndroidDriverFactory.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/seleniumtests/browserfactory/AndroidDriverFactory.java b/src/main/java/com/seleniumtests/browserfactory/AndroidDriverFactory.java index 13e8520..3bc1ea1 100644 --- a/src/main/java/com/seleniumtests/browserfactory/AndroidDriverFactory.java +++ b/src/main/java/com/seleniumtests/browserfactory/AndroidDriverFactory.java @@ -26,6 +26,7 @@ import com.seleniumtests.driver.DriverConfig; import io.appium.java_client.android.AndroidDriver; +import org.openqa.selenium.WebDriverException; /** * AndroidDriverFactory. @@ -60,7 +61,7 @@ public WebDriver createWebDriver() throws IOException { protected void setPageLoadTimeout(final long timeout) { try { driver.manage().timeouts().pageLoadTimeout(timeout, TimeUnit.SECONDS); - } catch (UnsupportedCommandException e) { + } catch (WebDriverException e) { } } From ca14e8b7faf5ae602fe81aeaf2ca4de9d1f01478 Mon Sep 17 00:00:00 2001 From: Malcolm Shen Date: Wed, 13 Jul 2016 13:51:48 +0800 Subject: [PATCH 4/5] Revert "Enhancement: add FairTestRetryAnalyzer to support retries per test level and max retries at global level." This reverts commit 384d889d0cabce511d9d18fdc4c520240e7010e3. --- .../core/FairTestRetryAnalyzer.java | 85 ------------------- .../core/FairTestRetryListener.java | 33 ------- .../core/ITestRetryAnalyzer.java | 10 --- .../seleniumtests/core/TestRetryAnalyzer.java | 5 +- .../reporter/SeleniumTestsReporter.java | 6 +- 5 files changed, 4 insertions(+), 135 deletions(-) delete mode 100644 src/main/java/com/seleniumtests/core/FairTestRetryAnalyzer.java delete mode 100644 src/main/java/com/seleniumtests/core/FairTestRetryListener.java delete mode 100644 src/main/java/com/seleniumtests/core/ITestRetryAnalyzer.java diff --git a/src/main/java/com/seleniumtests/core/FairTestRetryAnalyzer.java b/src/main/java/com/seleniumtests/core/FairTestRetryAnalyzer.java deleted file mode 100644 index cd551c7..0000000 --- a/src/main/java/com/seleniumtests/core/FairTestRetryAnalyzer.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2015 www.seleniumtests.com - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.seleniumtests.core; - -import org.testng.IRetryAnalyzer; -import org.testng.ITestResult; - -public class FairTestRetryAnalyzer implements IRetryAnalyzer, ITestRetryAnalyzer { - - public static final String TEST_RETRY_COUNT = "fairTestRetryCount"; - public static final String TEST_RETRY_MAX = "fairTestRetryMax"; - private int count = 1; - private int maxCount = 1; - - private int countLimit = 1; - private int maxCountLimit = 2; - - - public FairTestRetryAnalyzer() { - String retryCount = System.getProperty(TEST_RETRY_COUNT); - if (retryCount != null) { - countLimit = Integer.parseInt(retryCount); - } - - String retryMaxCount = System.getProperty(TEST_RETRY_MAX); - if (retryMaxCount != null) { - maxCountLimit = Integer.parseInt(retryMaxCount); - } - } - - public int getCount() { - return this.count; - } - - public int getMaxCount() { - return this.maxCount; - } - - public synchronized boolean retry(final ITestResult result) { - String testClassName = String.format("%s.%s", result.getMethod().getRealClass().toString(), - result.getMethod().getMethodName()); - - Integer retryCount = Integer.class.cast(result.getTestContext().getAttribute("fairTestRetryCount")); - if (retryCount == null) { - count = 1; - } else { - count = retryCount; - } - - if (count <= countLimit && maxCount <= maxCountLimit) { - - result.setAttribute("RETRY", new Integer(count)); - TestLogging.log("[RETRYING] " + testClassName + " FAILED, " + "Retrying " + count + " time", true); - result.getTestContext().setAttribute("fairTestRetryCount", ++count); - - maxCount++; - return true; - } - - return false; - } - - - public boolean retryPeek(final ITestResult result) { - Integer retryCount = Integer.class.cast(result.getTestContext().getAttribute("fairTestRetryCount")); - if (retryCount == null) { - count = 1; - } else { - count = retryCount; - } - - return count <= countLimit && maxCount <= maxCountLimit; - } -} diff --git a/src/main/java/com/seleniumtests/core/FairTestRetryListener.java b/src/main/java/com/seleniumtests/core/FairTestRetryListener.java deleted file mode 100644 index 550b54a..0000000 --- a/src/main/java/com/seleniumtests/core/FairTestRetryListener.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2015 www.seleniumtests.com - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * http://www.apache.org/licenses/LICENSE-2.0 - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.seleniumtests.core; - -import org.testng.IAnnotationTransformer; -import org.testng.IRetryAnalyzer; -import org.testng.annotations.ITestAnnotation; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; - -public class FairTestRetryListener implements IAnnotationTransformer { - - public void transform(final ITestAnnotation annotation, final Class testClass, final Constructor testConstructor, - final Method testMethod) { - IRetryAnalyzer retryAnalyzer = annotation.getRetryAnalyzer(); - if (retryAnalyzer == null) { - annotation.setRetryAnalyzer(FairTestRetryAnalyzer.class); - } - } - -} diff --git a/src/main/java/com/seleniumtests/core/ITestRetryAnalyzer.java b/src/main/java/com/seleniumtests/core/ITestRetryAnalyzer.java deleted file mode 100644 index 2670e00..0000000 --- a/src/main/java/com/seleniumtests/core/ITestRetryAnalyzer.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.seleniumtests.core; - -import org.testng.ITestResult; - - -public interface ITestRetryAnalyzer { - boolean retryPeek(ITestResult var1); - - int getCount(); -} diff --git a/src/main/java/com/seleniumtests/core/TestRetryAnalyzer.java b/src/main/java/com/seleniumtests/core/TestRetryAnalyzer.java index ccad0c9..865b9eb 100644 --- a/src/main/java/com/seleniumtests/core/TestRetryAnalyzer.java +++ b/src/main/java/com/seleniumtests/core/TestRetryAnalyzer.java @@ -16,7 +16,7 @@ import org.testng.IRetryAnalyzer; import org.testng.ITestResult; -public class TestRetryAnalyzer implements IRetryAnalyzer, ITestRetryAnalyzer { +public class TestRetryAnalyzer implements IRetryAnalyzer { private static final String TEST_RETRY_COUNT = "testRetryCount"; private int count = 1; @@ -57,7 +57,4 @@ public synchronized boolean retry(final ITestResult result) { return false; } - public boolean retryPeek(final ITestResult result) { - return count <= maxCount; - } } diff --git a/src/main/java/com/seleniumtests/reporter/SeleniumTestsReporter.java b/src/main/java/com/seleniumtests/reporter/SeleniumTestsReporter.java index 7a123ee..23396fe 100644 --- a/src/main/java/com/seleniumtests/reporter/SeleniumTestsReporter.java +++ b/src/main/java/com/seleniumtests/reporter/SeleniumTestsReporter.java @@ -81,7 +81,7 @@ import com.seleniumtests.core.SeleniumTestsContextManager; import com.seleniumtests.core.SeleniumTestsPageListener; import com.seleniumtests.core.TestLogging; -import com.seleniumtests.core.ITestRetryAnalyzer; +import com.seleniumtests.core.TestRetryAnalyzer; import com.seleniumtests.driver.ScreenShot; import com.seleniumtests.driver.ScreenshotUtil; @@ -1113,9 +1113,9 @@ public void onTestFailedButWithinSuccessPercentage(final ITestResult arg0) { } public synchronized void onTestFailure(final ITestResult arg0) { if (arg0.getMethod().getRetryAnalyzer() != null) { - ITestRetryAnalyzer testRetryAnalyzer = (ITestRetryAnalyzer) arg0.getMethod().getRetryAnalyzer(); + TestRetryAnalyzer testRetryAnalyzer = (TestRetryAnalyzer) arg0.getMethod().getRetryAnalyzer(); - if (testRetryAnalyzer.retryPeek(arg0)) { + if (testRetryAnalyzer.getCount() <= testRetryAnalyzer.getMaxCount()) { arg0.setStatus(ITestResult.SKIP); Reporter.setCurrentTestResult(null); } else { From 3d310a2d7fcff072d1c4791f0e2c64dcc9f05ca9 Mon Sep 17 00:00:00 2001 From: Malcolm Shen Date: Wed, 13 Jul 2016 15:21:33 +0800 Subject: [PATCH 5/5] Fixed the confusing comments in Android m-site Test --- testng.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testng.xml b/testng.xml index a25c07b..75fa87f 100755 --- a/testng.xml +++ b/testng.xml @@ -88,7 +88,7 @@ + Set the property "app" to "Chrome" to run test on chrome browser on android device instead of default Browser-->