Reputation: 573
Hi I have following setup with my current framework,
ClassA
{
//Which Receives Selenium WebDriver call the 'driver' object reference to manipulate the locators in UI
public WebDriver get()
{
return MainClass.driver;
}
}
MainClass
{
public static Webdriver driver;
method A()
{
//which uses Firefox instance and it is passed to ClassA to operate
driver = new FirefoxDriver();
}
methodB()
{
//which creates new instance of Chrome
driver = new ChromeDriver();
}
}
What I wanted to do is once I call methodB() the instance of Chrome is created but once it is done I want to resume back to firefox instance that is available or invoked before chrome run, but with my approach since I am referring the same webdriver object the old firefox reference is getting deleted.
Any Suggestions ?
PS: Please forgive my bad code conventions I followed
Upvotes: 0
Views: 2271
Reputation: 46
You might want to see a different approach to your situation. I believe that if you have to use 2 browsers you are most likely trying to pass some info from one to the other. Here is how I see it:
ClassA
{
//Which Receives Selenium WebDriver call the 'driver' object reference to manipulate the locators in UI
public WebDriver get()
{
return MainClass.driver;
}
}
MainClass
{
public static Webdriver currentBrowser, firefoxInstance chromeInstance;
firefoxInstance = new FirefoxDriver();
chromeInstance= new ChromeDriver();
currentBrowser = firefoxInstance; //if you want start out with Firefox
currentBrowser()
{
return currentBrowser;
}
switchBrowser(Cookies passingInfo) //passingInfo could be like cookies but also just current page etc...
{
if(currentBrowser==firefoxInstance)
{
chromeInstance.cookies()=passingInfo; // this is definitely not the correct way of passing cookies in Selenium but you get my point
currentBrowser=chromeInstance;
}
else
{
firefoxInstance.cookies()=passingInfo;
currentBrowser=firefoxInstance;
}
}
}
Of course there are more than one way of doing this and it depends on what you end goal is. But keep in mind that some websites are designed differently depending on the user agent of the browser connecting to them and that might cause your code to crash (like i just experienced 2 minutes ago.) I recommend sticking to one web browser if you can.
PS: Please forgive me using your bad code conventions :)
Upvotes: 0
Reputation: 1525
Simplest solution would be to create seperate objects for FF and Chrome. Modify get method to take a parameter(browserType) and then return the correspoding object.
Why are you switching browsers?
Upvotes: 7