0

I'm executing a test with multiple classes in sequence mean the login should be done dann the content should be executed, when run in one file everything work fine. but in two different classes the first work but the second is not reach. i don't know, what i'm doing wrong here!!! if it's my xml file needed to be configure or how to make the second class getting information from first class to execute test with.

My DriverManager class looks like this

package a_plus_a.selenium.Base;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class DriverManager {
    private static WebDriver myDriver;
    
    public static WebDriver getDriver(String browser) {
//  System.out.println("DriverManager:" + browser);
//  String portalURL = null;
//  BufferedReader br;  
    
/*  try {
        br = new BufferedReader(new FileReader("input.txt"));
        portalURL = br.readLine();
        System.out.println(portalURL);
        br.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace(); 
    } 
        
    try {
        getURL("input.txt");
        }
    catch (Exception e) {
        // TODO: handle exception
    } */
    
    switch (browser) {
    case "Chrome" : initializeChromeDriver();
                    break;
    case "Edge" : initializeEdgeDriver();
                break;
    case "Firefox": initializeFirefoxDriver();
                    break;
    default: System.out.println("Invalid browser name: " + browser);
             break;
    }
    
    myDriver.manage().deleteAllCookies();
    myDriver.manage().window().maximize();
    myDriver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
    myDriver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
    myDriver.get(getURL("input.txt"));
//  myDriver.get(getURL("input_INT.txt"));
    
    
    if (browser.contains("Edge")) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            System.out.println("Edge driver start error.");
        }
        myDriver.navigate().to("javascript:document.getElementById('overridelink').click()");
    }
    
    return myDriver;
    }

    public static String getURL(String url) {
        
        
        BufferedReader br1;
        String portalURL = null;
        
        try {
            FileReader file = new FileReader(url);
            br1 = new BufferedReader(file);
            portalURL = br1.readLine();
            System.out.println(portalURL);
            br1.close();
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace(); 
        }
        
        return portalURL;
        
    }
    private static void initializeChromeDriver() {
        System.out.println("Trying to initialize browser : ");
        System.setProperty("webdriver.chrome.driver", "ChromeDriver\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions(); 
        //options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
        options.addArguments("--remote-allow-origins=*");
        options.addArguments("--no-sandbox"); 
        myDriver = new ChromeDriver(options);
        myDriver.manage().window().maximize();
        System.out.println("initialization : " + myDriver);
    }
    
    private static void initializeEdgeDriver() {
        System.out.println("Trying to initialize browser : ");
        EdgeOptions options = new EdgeOptions();
        System.setProperty("webdriver.ie.driver", "EdgeDriver\\msedgedriver.exe");
        options.addArguments("--remote-allow-origins=*");
    //  options.addArguments("--no-sandbox");
        myDriver = new EdgeDriver(options);
        myDriver.manage().window().maximize();
        System.out.println("initialization : " + myDriver);
    }
    
    private static void initializeFirefoxDriver() {
        System.out.println("Trying to initialize browser : ");
        FirefoxOptions options = new FirefoxOptions();
        Map<String, Object> prefs = new HashMap<>();
        prefs.put("profile.default_content_settings.popups", 0);
        options.setCapability("prefs", prefs);
        System.setProperty("webdriver.gecko.driver", "FirefoxDriver\\geckodriver.exe");
        myDriver = new FirefoxDriver();
        System.out.println("initialization : " + myDriver);
    }
}

My Configuration file loos like this

package a_plus_a.selenium.Base;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.testng.Assert;
    import org.testng.ITestContext;
    import org.testng.annotations.*;

    public class ConfigurationFile {
    
    public WebDriver driver;
    private WebDriver webdriver;
    private String currentURL;
    
    
    
    @BeforeTest(description = "Launch site")
    @Parameters({"browser"})    
    public void setUpDriver(String browser) {           
        driver = DriverManager.getDriver(browser);
        System.out.println("je suis setUpDriver");
    }
    
    @BeforeClass
    public void storeDriver(ITestContext ctx) {
        ctx.setAttribute("driver", driver);
        System.out.println("Je suis storeDriver." + ctx.getAttribute("driver"));
    }
        
    @AfterTest(description = "Cleanup browser", alwaysRun = true)
    protected void tearDownAfterTestClass() {

        System.out.println("Browser closed. WebDriver stopped.");
        //driver.quit();
    }

    
    public WebDriver getWebdriver() {
        return webdriver;
    }

    public void setWebdriver(WebDriver webdriver) {
        this.webdriver = webdriver;
    }

    public String getCurrentURL() {
        return currentURL;
    }

    public void setCurrentURL(String currentURL) {
        this.currentURL = currentURL;
    }
    
    public ConfigurationFile()
    {
        
    }

    }

My first class looks like this

 package a_plus_a.selenium.Login;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.devtools.v112.runtime.model.WebDriverValue;
    import org.testng.Assert;
    import org.testng.ITestContext;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;

    import a_plus_a.selenium.Base.ConfigurationFile;

    public class Login_TestUser extends ConfigurationFile {
    
     // private WebDriver webdriver;
    //  String currentURL;
    //  BaseTest basetest = new BaseTest();
    String actualURL = "https://keycloak.ldi.an-environment.dev";
    String elsterURL = "https://e4k-portal.een.elster.de/";
    public static final String LOGIN = "login";
    public static final String USERNAME = "username";
    public static final String PASSWORD = "password";

    @BeforeClass
    public void getCtx(ITestContext ctx) {
    //  this.basetest.setWebdriver((WebDriver) ctx.getAttribute("driver"));
        WebDriver browser = (WebDriver)ctx.getAttribute("driver");
        System.out.println("ctx login:" + ctx.getAttribute("driver"));
        setWebdriver(browser);
    }
    
    
    @Test (description = "Click auf Anmelden", priority = 1)
    public void Anmelden() {
        try {
        Thread.sleep(2000);
        getWebdriver().findElement(By.xpath("/html/body/app-root/div/div/app-                                start/div/app-home/div/div[1]/button")).click();
        Thread.sleep(2000);
        setCurrentURL(getWebdriver().getCurrentUrl());
        System.out.println("current URL: " + getCurrentURL());
        Assert.assertTrue(getCurrentURL().contains("/anmeldung"));
        } catch (Exception e) {
        Assert.fail();
        e.printStackTrace();
        }
    }
    
     @Test(description = "Enter valid login data", priority = 2)
        public void TC0202_LoginPage() {
            try {
            getWebdriver().findElement(By.xpath("//*[@id=\"input-   0\"]")).sendKeys("test@newuser.de");    
            getWebdriver().findElement(By.xpath("//*[@id=\"input-   1\"]")).sendKeys("45345");     
            getWebdriver().findElement(By.xpath("/html/body/app-root/div/div/app-  login/button")).click();
            Thread.sleep(2000);
            setCurrentURL(getWebdriver().getCurrentUrl());
            System.out.println("current URL: " + getCurrentURL());
            Assert.assertTrue(getCurrentURL().contains("/portal"));
            } catch (Exception e) {
                Assert.fail();
                e.printStackTrace();
                }           
            
     } 
    
    
    }

My second class looks like this

package a_plus_a.selenium.Login;

    import static org.testng.Assert.assertEquals;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.testng.Assert;
    import org.testng.ITestContext;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;

    import a_plus_a.selenium.Base.ConfigurationFile;

    public class Onboarding extends ConfigurationFile {
    
    
    @BeforeTest
    public void getCtx(ITestContext ctx) {
        WebDriver browser = (WebDriver)ctx.getAttribute("driver");
        System.out.println("ctx onboardig:" + ctx.getAttribute("driver") + browser);
        setWebdriver(browser);
    }
    
     
    @Test (description = "Click auf 'Weiter' Button auf Popup", priority = 3,    dependsOnMethods = {"TC0202_LoginPage"})
    public void Popup_Durchlesen() throws InterruptedException  {
        Login_TestUser login = new Login_TestUser();
        System.out.println("current URL onboarding login: " + login.getCurrentURL());
        try {
            if (login.getCurrentURL().contains("/portal"))
            {           
            setCurrentURL(getWebdriver().getCurrentUrl());
            System.out.println("current URL onboarding: " + getCurrentURL());
            Thread.sleep(3000);
            getWebdriver().findElement(By.xpath("/html/body/div[2]/div[2]/div/mat-  dialog-container/div/div/mat-checkbox/label/span[1]")).click();
            Thread.sleep(2000);
            getWebdriver().findElement(By.xpath("/html/body/div[2]/div[2]/div/mat-  dialog-container/app-onboarding-modal/button")).click();
            Thread.sleep(2000);
            getWebdriver().findElement(By.xpath("/html/body/div[2]/div[2]/div/mat-  dialog-container/app-onboarding-modal/div//button")).click();
            Thread.sleep(2000);
            getWebdriver().findElement(By.xpath("/html/body/div[2]/div[2]/div/mat-dialog-container/app-onboarding-modal/div/button")).click();
            Thread.sleep(2000);
            setCurrentURL(getWebdriver().getCurrentUrl());
            System.out.println("current URL: " + getCurrentURL());
            Assert.assertTrue(getCurrentURL().contains("/portal"));
            }
            
            else {
                System.out.println("current URL onboarding: is not successful" +   getCurrentURL());
            }
            
            } catch (Exception e) {
            Assert.fail();
            e.printStackTrace();
        }
    }
    

My Testng.xml file looks like this

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite name="Suite" thread-count="1" parallel="tests">
        <test verbose="1" name="MytestCase" allow-return-values="true">
    <parameter name="browser" value="Chrome"> </parameter>
           <classes>
        <class name="a_plus_a.selenium.Login.Login_TestUser"/>
                <class name="a_plus_a.selenium.Login.Onboarding"/> 
            <class name="a_plus_a.selenium.Base.ConfigurationFile"/> 
           </classes>
        </test> <!-- Test -->
    </suite> <!-- Suite -->

On the console, i receive this message:

Trying to initialize browser : 
    Aug. 13, 2023 6:13:01 PM org.openqa.selenium.remote.service.DriverService$Builder  getLogOutput
    INFORMATION: Driver logs no longer sent to console by default;     https://www.selenium.dev/documentation/webdriver/drivers/service/#setting-log-output
    Aug. 13, 2023 6:13:02 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
    WARNUNG: Unable to find an exact match for CDP version 115, so returning the closest version  found: 114
    initialization : ChromeDriver: chrome on windows (bcf82d21d3285358f1dfc727a27b710d)
    https://nf.form-demo.de/de/
    je suis setUpDriver
    ctx onboardig:nullnull
    Je suis storeDriver.ChromeDriver: chrome on windows (bcf82d21d3285358f1dfc727a27b710d)
    ctx login:ChromeDriver: chrome on windows (bcf82d21d3285358f1dfc727a27b710d)
    current URL: https://nf.form-demo.de/anmeldung
    current URL: https://nf.form-demo.de/portal
    Je suis storeDriver.null
    current URL onboarding login: null
    Je suis storeDriver.null
    ctx Form44:null
    Browser closed. WebDriver stopped.

    ===============================================
    Suite
    Total tests run: 3, Passes: 2, Failures: 1, Skips: 0
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Aug 13 '23 at 20:16
  • Are you looking for something like this https://stackoverflow.com/questions/2669576/order-of-execution-of-tests-in-testng ? – oturan Aug 15 '23 at 17:55

0 Answers0