1

I am trying to integrate testng with testrail but I am having a problem. Below the code is in my BaseTest. If I run testng on BaseTest it works and I see my result in testrail. If I move onboardingtest, changingToTest, and changingToAllListings to its own class, running testng will bomb the @BeforeMethod. I am not sure how to fix it. Can someone help? My guess is something wrong with @BeforeMethod. Maybe it shouldn't be Method m = BaseTest.class.getMethod(method.getName()); I have attached my error as well

package appiumStudio.appiumStudio;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import org.json.simple.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.*;

import api.APIException;
import api.APIClient;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import pageObjects.Onboarding;
import pageObjects.Search;
import util.OnboardingFlow;


public class BaseTest {
    protected static AndroidDriver<AndroidElement> driver = null;
    String PROJECT_ID = "13";
 APIClient client = null;
    DesiredCapabilities dc = new DesiredCapabilities();
    
    @BeforeSuite
     public void createSuite(ITestContext ctx) throws MalformedURLException,IOException, APIException {

        dc.setCapability(MobileCapabilityType.UDID, "6bf9c570");
        dc.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.test.buyerapp.staging");
        dc.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.tet.buyerapp.MainActivity");
        driver = new AndroidDriver<AndroidElement>(new URL("http://localhost:4723/wd/hub"), dc);
        driver.setLogLevel(Level.INFO);
        
        client = new APIClient("https://test.testrail.io");
  client.setUser("qa@test.com");
  client.setPassword("password");
  Map data = new HashMap();
  data.put("include_all",true);
  data.put("name","Test Run "+System.currentTimeMillis());
  JSONObject c = null;
  c = (JSONObject)client.sendPost("add_run/"+PROJECT_ID,data);
  Long suite_id = (Long)c.get("id");
  ctx.setAttribute("suiteId",suite_id);
        
     driver.resetApp();

    }
    
    @BeforeMethod
 public void beforeTest(ITestContext ctx,Method method) throws NoSuchMethodException {
  Method m = BaseTest.class.getMethod(method.getName());

  if (m.isAnnotationPresent(TestRails.class)) {
   TestRails ta = m.getAnnotation(TestRails.class);
   System.out.println(ta.id());
   ctx.setAttribute("caseId",ta.id());
  }
 }
    
    @TestRails(id="20178")
 @Test(priority=1)
 public void onboardingtest() throws InterruptedException {
  OnboardingFlow.onboarding();
 }
 @TestRails(id="20176")
 @Test(priority = 2)
 public void changingToTest() throws InterruptedException {
  Search.ToggleAllListings(driver).click();
  new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='Every available home for sale']")));
  Search.SelectTestHomes(driver).click();
  new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='Self-tour Test homes']")));
  Search.ClickGotIt(driver).click();
  Thread.sleep(2000);
 }
    @TestRails(id="20177")
 @Test(priority = 3)
 public void changingToAllListings() {
  Search.ToggleTestHomes(driver).click();
  new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='Self-tour instantly with app unlock']")));
  Search.SelectAllListings(driver).click();
  new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@text='All listings']")));
 }
    
    @AfterMethod
    public void afterTest(ITestResult result, ITestContext ctx) throws IOException, APIException {
     Map data = new HashMap();
     if(result.isSuccess()) {
      data.put("status_id",1);
     }
     else {
      data.put("status_id", 5);
      data.put("comment", result.getThrowable().toString());
     }

     String caseId = (String)ctx.getAttribute("caseId");
     Long suiteId = (Long)ctx.getAttribute("suiteId");
     client.sendPost("add_result_for_case/"+suiteId+"/"+caseId,data);

    }
    
    @AfterClass
    public void tearDown() {
     driver.resetApp();

    }
}

I get this error below whenI move onboardingtest, changingToTest, and changingToAllListings to its own class.

FAILED: beforeTest(org.testng.TestRunner@578524c3, public void searchHomeToggle.ToggleHome.beforeTest(org.testng.ITestContext,java.lang.reflect.Method) throws java.lang.NoSuchMethodException) java.lang.NoSuchMethodException: appiumStudio.appiumStudio.BaseTest.beforeTest()
at java.base/java.lang.Class.getMethod(Class.java:2114) at searchHomeToggle.ToggleHome.beforeTest(ToggleHome.java:23) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584) at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172) at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.testng.TestRunner.privateRun(TestRunner.java:770) at org.testng.TestRunner.run(TestRunner.java:591) at org.testng.SuiteRunner.runTest(SuiteRunner.java:402) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
at org.testng.SuiteRunner.run(SuiteRunner.java:304) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180) at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
at org.testng.TestNG.runSuites(TestNG.java:1032) at org.testng.TestNG.run(TestNG.java:1000) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Jackie Ngo
  • 11
  • 1

0 Answers0