ericmckean / chromedriver

Automatically exported from code.google.com/p/chromedriver
0 stars 0 forks source link

#window_handle error #950

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
On a closed window, calling driver.window_handle should raise 
Selenium::WebDriver::Error::NoSuchWindowError

(This code passes as expected in Firefox)

Interestingly, if you call driver.window_handles before driver.window_handle, 
the exception is thrown as expected.

Passing:
driver = Selenium::WebDriver.for :chrome
        @html = File.expand_path(File.dirname(__FILE__) + '/html/window_switching.html')
        driver.navigate.to "file://#{@html}"
        driver.first(id: 'open').click
        driver.switch_to.window(driver.window_handles.last)
        driver.first(id: 'close').click
        closed_window = driver.window_handle
        expect(driver.window_handles.include? closed_window).to be false
        expect {driver.window_handle}.to raise_error(Selenium::WebDriver::Error::NoSuchWindowError)

Failing:
driver = Selenium::WebDriver.for :chrome
        @html = File.expand_path(File.dirname(__FILE__) + '/html/window_switching.html')
        driver.navigate.to "file://#{@html}"
        driver.first(id: 'open').click
        driver.switch_to.window(driver.window_handles.last)
        driver.first(id: 'close').click
        expect {driver.window_handle}.to raise_error(Selenium::WebDriver::Error::NoSuchWindowError

<!DOCTYPE HTML>
<html>
  <head>
    <title>window switching</title>
  </head>

  <body>
    <p>
      Click <a id="open" href="#" onclick='window.open("closeable.html")'>here</a> to open a new window.
    </p>
  </body>
</html>

<!DOCTYPE HTML>
<html>
  <head>
    <title>closeable window</title>
  </head>

  <body>
    <p>
      Click <a id="close" href="#" onclick="window.close()">here</a> to close this window.
    </p>
  </body>

</html>

Original issue reported on code.google.com by titusfor...@gmail.com on 21 Oct 2014 at 11:34

GoogleCodeExporter commented 9 years ago

Original comment by andrewch...@chromium.org on 24 Nov 2014 at 10:29

GoogleCodeExporter commented 9 years ago
1. Are you still having this problem?
2. What language is it?
3. Can you provide me standalone program that I can run directly to reproduce
   the problem?

Original comment by andrewch...@chromium.org on 24 Nov 2014 at 10:48

GoogleCodeExporter commented 9 years ago
1) Yes
2) Ruby
3) Here is a failing test I wrote for the Selenium Project, is this sufficient?

https://github.com/titusfortner/selenium/blob/1c3aec9ad981d7bb3af8d3ad6ef98154c9
297ded/rb/spec/integration/selenium/webdriver/target_locator_spec.rb#L46

Original comment by titusfor...@gmail.com on 24 Nov 2014 at 11:39

GoogleCodeExporter commented 9 years ago

According to
Passing:
driver = Selenium::WebDriver.for :chrome
        @html = File.expand_path(File.dirname(__FILE__) + '/html/window_switching.html')
        driver.navigate.to "file://#{@html}"
        driver.first(id: 'open').click
        driver.switch_to.window(driver.window_handles.last)
        driver.first(id: 'close').click
        closed_window = driver.window_handle
        expect(driver.window_handles.include? closed_window).to be false
        expect {driver.window_handle}.to raise_error(Selenium::WebDriver::Error::NoSuchWindowError)

Failing:
driver = Selenium::WebDriver.for :chrome
        @html = File.expand_path(File.dirname(__FILE__) + '/html/window_switching.html')
        driver.navigate.to "file://#{@html}"
        driver.first(id: 'open').click
        driver.switch_to.window(driver.window_handles.last)
        driver.first(id: 'close').click
        expect {driver.window_handle}.to raise_error(Selenium::WebDriver::Error::NoSuchWindowError

 any way you can minimize it to less than 20 line of codes?  I have zero experience on Ruby.

Original comment by andrewch...@chromium.org on 24 Nov 2014 at 11:55

GoogleCodeExporter commented 9 years ago
Ok, I'm not a Java dev, and can't really test this, but I'm following the 
pattern I see here at this link, and translating the Ruby calls into Java:
https://github.com/SeleniumHQ/selenium/blob/master/java/client/test/org/openqa/s
elenium/WindowSwitchingTest.java

  @Test
  public void testShouldThrowNoSuchWindowExceptionForClosedWindow() {
    driver.get(pages.xhtmlTestPage);
    driver.findElement(By.linkText("Open new window")).click();
    driver.switchTo().window("result");
    String currentHandle = driver.getWindowHandle();
    driver.findElement(By.id("close")).click();

    try {
      driver.switchTo().window(currentHandle);
      fail("NoSuchWindowException expected");
    } catch (NoSuchWindowException e) {
      // Expected.
    }

  }

The passing test I gave for reference was only to say that if you do 
driver.getWindowHandles(); after closing the second window with the link, and 
before trying to switch to it, chrome returns the desired error.

Original comment by titusfor...@gmail.com on 25 Nov 2014 at 12:31

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
eventually,  I need test the Rubycodes which you are having an issue

Failing:
driver = Selenium::WebDriver.for :chrome
        @html = File.expand_path(File.dirname(__FILE__) + '/html/window_switching.html')
        driver.navigate.to "file://#{@html}"
        driver.first(id: 'open').click
        driver.switch_to.window(driver.window_handles.last)
        driver.first(id: 'close').click
        expect {driver.window_handle}.to raise_error(Selenium::WebDriver::Error::NoSuchWindowError

 Please provide me a small complete codes that I can run and reproduce the problem.

Original comment by andrewch...@chromium.org on 25 Nov 2014 at 6:21

GoogleCodeExporter commented 9 years ago
I'm not sure what you mean by a complete codes. I linked to a test I wrote for 
the Ruby bindings in the Selenium project that fails as a result of this bug. 
It includes the code that causes it to fail and makes use of the 2 html files 
required to reproduce the bug. Do you want a separate github project that just 
has those 2 files and that test?

Original comment by titusfor...@gmail.com on 25 Nov 2014 at 6:28

GoogleCodeExporter commented 9 years ago

If you can provide me less than 20 lines of Ruby codes.  Similar to Java,  I 
run the whole case with not more than 20 lines.  This way make me easier to 
trace.

Original comment by andrewch...@chromium.org on 25 Nov 2014 at 6:44

GoogleCodeExporter commented 9 years ago
Paste this in irb to see the unexpected failure:
https://gist.github.com/titusfortner/a5a6c6dae66bc0cbd3b3

Un-comment out the designated line to see the test pass

Original comment by titusfor...@gmail.com on 25 Nov 2014 at 10:58

GoogleCodeExporter commented 9 years ago
I have codes in Java
            driver.get("file:///C:/Chromium_test_case/issue_950.html");
            // driver.get("file:///C:/customers/ckeditor/samples/appendto.html");
            driver.findElement(By.id("open")).click();
            // Store the current window handle
            String winHandleBefore = driver.getWindowHandle();
            // driver.switchTo().window("result");
            // Switch to new window opened
            String winHandleLast  =  null;
            for (String winHandle : driver.getWindowHandles()) {
                driver.switchTo().window(winHandle);
                winHandleLast = winHandle;
            }
            //
            // Switch back to original browser (first window)
            // close open window
            driver.findElement(By.id("close")).click();

can you help to make your Ruby match above codes and make it ready to go.
I am  zero experience, and don't know anything about Ruby.

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 12:54

GoogleCodeExporter commented 9 years ago
I'm sorry, I don't understand what you are asking for now.

Do you want me to change this Java code so you can see the failure in Java? Or 
do you want me to change the Ruby code I wrote to follow the Java pattern above?

Original comment by titusfor...@gmail.com on 26 Nov 2014 at 1:09

GoogleCodeExporter commented 9 years ago
based on my Java setup

if you can change your following Ruby code use my setup. (HTML, chromedriver 
path etc...)

            System.setProperty("webdriver.chrome.driver",
                    "C:\\Chromium\\src\\out-dynamic\\Debug\\chromedriver.exe");
            // System.setProperty("webdriver.chrome.logfile", new
            // File("chromedriver494.log").getAbsolutePath());
            driver = new ChromeDriver(new ChromeOptions());

            //
            driver.get("file:///C:/Chromium_test_case/issue_950.html");
            // driver.get("file:///C:/customers/ckeditor/samples/appendto.html");
            driver.findElement(By.id("open")).click();
            // Store the current window handle
            String winHandleBefore = driver.getWindowHandle();

  so that I can just run your codes without change any

module Issue950

  require 'selenium-webdriver'
  first_window = File.open('issue_950.html', 'w').tap {|f| f.puts "<html><head><title><Window1</title></head><body><p>Click <a id='open' href='#' onclick='window.open(\"window2.html\")'>here</a></body></html>" }
  first_window.close
  File.open('window2.html', 'w').tap {|f| f.puts "<html><head><title>window1</title></head><body><p>Click <a id='close' href='#' onclick=\"window.close()\">close</a></p></body></html>" }.close
  driver = Selenium::WebDriver.for :chrome
  driver.navigate.to "file://#{File.expand_path(File.dirname(__FILE__))}/#{first_window.path}"
  driver.first(id: 'open').click
  driver.switch_to.window(driver.window_handles.last)
  driver.first(id: 'close').click
     #driver.window_handles ### If you un-comment out this line, the test will pass
  begin
    driver.window_handle
    raise "This test did not throw the expected NoSuchWindowError"
  rescue Selenium::WebDriver::Error::NoSuchWindowError
    puts "This test passes as expected!"
  end
end

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 2:21

GoogleCodeExporter commented 9 years ago
The code in the file I already linked is as ready to go as possible without any 
need to change anything; I even removed the need to create specific html files 
ahead of time.

Setup requirements:

1) ChromeDriver. Only Java needs to set the location of Chromedriver; for 
everything else you just make sure its location is accessible in PATH
https://sites.google.com/a/chromium.org/chromedriver/getting-started

2) Ruby. I haven't ever installed Ruby on Windows, it looks like the 
recommendation is to use Rubyinstaller - 
http://stackoverflow.com/questions/18908708/installing-ruby-gem-in-windows

3). Selenium. Open a windows console or terminal and run:
gem install selenium-webdriver

4) IRB. Interactive Ruby is the easiest way to execute Ruby scripts. It looks 
like fxri might be the windows equivalent? I'm not sure, I've never used it - 
https://www.ruby-lang.org/en/documentation/quickstart/

Once you're in IRB, just copy & paste this code
https://gist.github.com/titusfortner/a5a6c6dae66bc0cbd3b3

Original comment by titusfor...@gmail.com on 26 Nov 2014 at 4:57

GoogleCodeExporter commented 9 years ago
commented/uncommented  
driver.window_handles ### If you un-comment out this line, the test will pass

The results remain the same

C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.44.0/lib/selenium/webdri
ver/remote/response.rb:52:in `assert_ok': no such element 
(Selenium::WebDriver::Error::NoSuchElementError)
  (Session info: chrome=39.0.2171.65)
  (Driver info: chromedriver=2.12 (4ddd65c18b72f36483ac11fef001596703118fb2),platform=Windows NT 6.1 SP1 x86_64)
    from C:/Ruby21/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 7:25

GoogleCodeExporter commented 9 years ago
Which line is giving the error? It probably isn't finding the element for
either the open or close link for some reason. Are they visible in the
browser?

Original comment by titusfor...@gmail.com on 26 Nov 2014 at 7:30

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
1. Browser show  following
file:///C:/ChromeClient_WS/SeleniumRubyClient/src/window1.html
This webpage is not found

when crash, is it normal?

2.   debugger is not a build-in.  need get one.
ran your case in Java(two HTMLs), and don't see any problem yet.

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 7:59

GoogleCodeExporter commented 9 years ago
The Ruby code should create the files window1.html and window2.html - are they 
present in that directory?

I really like RubyMine, which is pretty much just a specialized version of 
IntelliJ

Is this the Java code you are using?

  @Test
  public void testShouldThrowNoSuchWindowExceptionForClosedWindow() {
    driver.get("file:///C:/Chromium_test_case/issue_950.html");
    driver.findElement(By.linkText("Open new window")).click();
    driver.switchTo().window("result");
    String currentHandle = driver.getWindowHandle();
    driver.findElement(By.id("close")).click();

    try {
      driver.switchTo().window(currentHandle);
      fail("NoSuchWindowException expected");
    } catch (NoSuchWindowException e) {
      // Expected.
    }

  }

Original comment by titusfor...@gmail.com on 26 Nov 2014 at 8:08

GoogleCodeExporter commented 9 years ago
see above #11

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 8:26

GoogleCodeExporter commented 9 years ago
Try this:

driver.get("file:///C:/Chromium_test_case/issue_950.html");
driver.findElement(By.id("open")).click();
String winHandleLast  =  null;
for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle);
    winHandleLast = winHandle;
}
driver.findElement(By.id("close")).click();
driver.switchTo().window(winHandleLast)

Original comment by titusfor...@gmail.com on 26 Nov 2014 at 8:34

GoogleCodeExporter commented 9 years ago
it works the way it should be.
Has error when switch to a closed window

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 8:58

GoogleCodeExporter commented 9 years ago
What about this?

driver.get("file:///C:/Chromium_test_case/issue_950.html");
driver.findElement(By.id("open")).click();
String winHandleLast  =  null;
for (String winHandle : driver.getWindowHandles()) {
    driver.switchTo().window(winHandle);
    winHandleLast = winHandle;
}
driver.findElement(By.id("close")).click();
driver.getWindowHandle();

Original comment by titusfor...@gmail.com on 26 Nov 2014 at 9:02

GoogleCodeExporter commented 9 years ago
no error,  it get the main window handle.

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 10:30

GoogleCodeExporter commented 9 years ago
That *should error with NoSuchWindowError since the window the driver is 
currently referencing has been closed.

Can you verify for me what error you get when you run the code in #22?

Thanks!

Original comment by titusfor...@gmail.com on 26 Nov 2014 at 10:46

GoogleCodeExporter commented 9 years ago
why should error with NoSuchWindowError ?  there is a window available - the 
first one

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 11:09

GoogleCodeExporter commented 9 years ago
The reference it returns isn't to the active window, but to the closed window. 
Try it with this at the end instead:
driver.getCurrentUrl(); 

Regardless, the spec is pretty explicit about what to return before a new top 
level browsing context is defined- 
https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html#methods-4

The fact that Ruby & Java give the same result here is good, though.
I still want to understand why your java code is returning something different 
with respect to the original bug. 
What is the specific error you are getting from this line in post #22?
driver.switchTo().window(winHandleLast)

Original comment by titusfor...@gmail.com on 26 Nov 2014 at 11:23

GoogleCodeExporter commented 9 years ago
yes, the reference  is the one which closed - may not be right
getWindowHandles() will give the available.
I will look into it.

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 11:27

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
in #22
there are many errors

           // not work -  driver.findElement(By.linkText("open a new window")).click();
            driver.findElement(By.linkText("here")).click();  // works

            driver.switchTo().window("result"); // not work 

Original comment by andrewch...@chromium.org on 26 Nov 2014 at 11:56

GoogleCodeExporter commented 9 years ago

Original comment by samu...@chromium.org on 21 Feb 2015 at 12:19