0

I have created some test cases in Eclipse. But while running the testng.xml file it opens in a new browser every time. How do I run it in single browser? I am calling below for each test case, so it opens in a new browser each time.

@BeforeClass
    public static void setup()
    {           
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium Jar\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-extensions");
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();            
    }
3

Define the webdriver at the class level and then call the same webdriver instance on each and every test method.

| improve this answer | |
0

You can write the below code when you would like to open a new Chrome browser. Feel free to change the port number to your desired number.

Then in that browser window you can make desired changes if you like OR can run your next test case. For running your next test case, please paste the second code provided in your test case.

System.setProperty("webdriver.chrome.driver", "//path of your Chrome Driver");
ChromeOptions options_1 = new ChromeOptions();
options_1.addArguments("--remote-debugging-port=9088");//port number
WebDriver driver = new ChromeDriver(options_1);

...Remaining selenium code...

Once your browser is open you are on the desired page and would like to call your open browser window in your test, you can use the below code for that.

System.setProperty("webdriver.chrome.driver", "//path of your Chrome Driver");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "localhost:9088");//port number
WebDriver driver = new ChromeDriver(options);

...Remaining selenium code...

Please make sure that your port number is same in all your test case.

| improve this answer | |
0

Try this out.

@BeforeTest
    public static void beforeMethod() {
    driver= new FirefoxDriver();
    }
    @Test
    public void main1() {
    driver.get("http://google.co.in/gmail_login.html");
    driver.findElement(By.id("element")).sendKeys("1111111111");
    String captcha = driver.findElement(By.id("txtCaptchaDiv")).getText();
    System.out.println("Captcha is: " +captcha);
    driver.findElement(By.id("txtInput")).sendKeys(captcha);
    }
    @Test
    public void main2() {
    driver.findElement(By.className("btn_style")).click();
    }
    @Test
    public void main3() {
    System.out.println("The title is: " +driver.getTitle());
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.