Use same instance of browser for multiple Custom Keywords

Hi, I’m trying to run Custom Keywords with same instance of browser like/in test cases, but on second keyword (Logout) it open a new browser window.
The code look like this

package kayword

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebU

public class Manuel {
	WebDriver driver = new ChromeDriver();

	@Keyword
	def Login(String url, String login, String pw){
		driver.manage().window().maximize();
		driver.get(url);
		driver.findElement(By.name("username")).sendKeys(login);
		driver.findElement(By.name("password")).sendKeys(pw);
		driver.findElement(By.xpath("//button[contains(text(), 'Connexion')]")).click();
	}
	@Keyword
	def Lougout(){
		//Thread.sleep(2000);
		driver.findElement(By.xpath("//span[@id='HEADER_USER_MENU_POPUP_text']")).click();
		driver.findElement(By.xpath("//td[@id='HEADER_USER_MENU_LOGOUT_text']")).click();
		driver.quit();
	}
}

Thanks in advance

Your code shows a class with two methods.

Where is the test case that uses the two methods? Can you post that as well?

I’m just calling the 2 keywords in test cases

Your class Manual has a line new ChromeDriver(). This is not good. This line is called when your test case calls CustomKeywords.'classname.Logout'(). You should move this line out of the Keyword class, and instead write it in you test case script.

And you should modify the method signature of your Keyword class so that it accepts WebDriver instance as argument from Test Case.

    def Login(WebDriver driver, String url, String login, String pw) {
    ....
    def Logout(WebDriver driver) {

But if I add (WebDriver driver) as parameter, what it should be value type /

Sorry, I do not see what the ‘Input’ screen of which screenshot you attached.

I never use the Manual mode of Test Case editor, I always use the Script mode.

Here’s how it look like in script

but I didn’t understood what do you mean by

new ChromeDriver() . This is not good. This line is called when your test case calls CustomKeywords.'classname.Logout'() . You should move this line out of the Keyword class, and instead write it in you test case script.

Try this.

The Keyword class:

package kaywords

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver

public class Keyword {

	def login(WebDriver driver, String url, String login, String pw) {
		driver.manage().window().maximize()
		driver.get(url)
		driver.findElement(By.name("username")).sendKeys(login);
		driver.findElement(By.name("password")).sendKeys(pw);
		driver.findElement(By.xpath("//button[contains(text(), 'Login')]")).click();
	}

	def logout(WebDriver driver) {
		Thread.sleep(2000)
		driver.quit()
	}
}

The Test Case script:

import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver

import com.kms.katalon.core.webui.driver.DriverFactory

String chromeDriverPath = DriverFactory.getChromeDriverPath()
System.setProperty('webdriver.chrome.driver', chromeDriverPath)

WebDriver driver = new ChromeDriver()

CustomKeywords.'kaywords.Keyword.login'(driver, 
		"https://katalon-demo-cura.herokuapp.com/profile.php#login",
		'John Doe',
		'ThisIsNotAPassword')

CustomKeywords.'kaywords.Keyword.logout'(driver)
// or similarly
//new kaywords.Keyword().logout(driver)

Thank you it works like charm