Skip to Content
Author's profile photo Stefan Schnell

Different Behaviour of the Chrome Browser in the Context of Selenium Script Recording and Replaying

To record and replay activities in the web browser I use Selenium, in conjunction with SAP GUI Scripting. But in some cases the browser shows a different behaviour between the recording and the replaying.

Here an example. I start the SAP Support Portal and click on Log On icon. On the same site opens a log on screen.

 

Here the recorded code in PowerShell:

#-Begin-----------------------------------------------------------------

  $Path = "C:\Program Files\Selenium"

  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\Selenium.WebDriverBackedSelenium.dll")
  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\WebDriver.dll")
  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\WebDriver.Support.dll")

  $Options = New-Object OpenQA.Selenium.Chrome.ChromeOptions
  $Options.BinaryLocation = "C:/Program Files/Google/Chrome/Application/chrome.exe"

  $driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($Path, $Options)

  $baseURL = "https://support.sap.com/en/index.html"
  $driver.Navigate().GoToUrl($baseURL); 
  $driver.FindElementByXPath("//section[@id='section_1528579655']/div/div/div/ul/li[3]/div/div/a/span").Click();
  $driver.FindElementById("j_username").Clear();
  $driver.FindElementById("j_username").SendKeys("S0123456789");
  $driver.FindElementById("j_password").Clear();
  $driver.FindElementById("j_password").SendKeys("secret");
  $driver.FindElementById("logOnFormSubmit").Click();

  $driver.Close()
  $driver.Quit()

#-End-------------------------------------------------------------------

 

But if I replay the script, the Chrome Browser opens a new tab with the logon screen:

 

In this case the script can’t find the elements username, password etc. So it is necessary to adjust the script.

#-Begin-----------------------------------------------------------------

  $Path = "C:\Program Files\Selenium"

  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\Selenium.WebDriverBackedSelenium.dll")
  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\WebDriver.dll")
  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\WebDriver.Support.dll")

  $Options = New-Object OpenQA.Selenium.Chrome.ChromeOptions
  $Options.BinaryLocation = "C:/Program Files/Google/Chrome/Application/chrome.exe"

  $driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($Path, $Options)

  $baseURL = "https://support.sap.com/en/index.html"
  $driver.Navigate().GoToUrl($baseURL); 
  $driver.FindElementByXPath("//section[@id='section_1528579655']/div/div/div/ul/li[3]/div/div/a/span").Click();

  $hWin = $driver.WindowHandles[1]
  $driver.SwitchTo().Window($hWin)

  $driver.FindElementById("j_username").Clear();
  $driver.FindElementById("j_username").SendKeys("S0123456789");
  $driver.FindElementById("j_password").Clear();
  $driver.FindElementById("j_password").SendKeys("secret");
  $driver.FindElementById("logOnFormSubmit").Click();

  $driver.Close()
  $driver.Quit()

#-End-------------------------------------------------------------------

I add code to switch to the next tab, now the script works perfect.

 

If you replay this kind of scripts on the first time, after a recording, it seems good to watch the automated activities carefully. And if the browser opens a new tab at a point where there was none at the recording, it is necessary to add code to switch to the tab.

 

2017/08/16: Here an example with PhantomJS

#-Begin-----------------------------------------------------------------

  $Path = "C:\Program Files\Selenium";

  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\Selenium.WebDriverBackedSelenium.dll");
  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\WebDriver.dll");
  [System.Reflection.Assembly]::LoadFrom($Path + "\dotNET\WebDriver.Support.dll");

  $Options = New-Object OpenQA.Selenium.PhantomJS.PhantomJSOptions;

  $driver = New-Object OpenQA.Selenium.PhantomJS.PhantomJSDriver($Path, $Options);

  $baseURL = "https://support.sap.com/en/index.html";
  $driver.Navigate().GoToUrl($baseURL); 
  $driver.FindElementById("headerLoginLink").Click(); 
  $driver.SwitchTo().Frame("https://support.sap.com/bin/fiji/es/login.support.html?_=nc#https%3A%2F%2Fsupport.sap.com%2Fen%2Findex.html");
  $driver.FindElementById("j_username").Clear();
  $driver.FindElementById("j_username").SendKeys("S0123456789");
  $driver.FindElementById("j_password").Clear();
  $driver.FindElementById("j_password").SendKeys("secret");
  $driver.FindElementById("logOnFormSubmit").Click();

  $SnapShot = $driver.GetScreenshot();
  $SnapShot.SaveAsFile($Path + "\Result.jpg", [OpenQA.Selenium.ScreenshotImageFormat]::Jpeg);

  $driver.Close();
  $driver.Quit();

#-End-------------------------------------------------------------------

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.