|  | 
|  | 1 | +# frozen_string_literal: true | 
|  | 2 | + | 
|  | 3 | +require 'selenium-webdriver' | 
|  | 4 | + | 
|  | 5 | +RSpec.describe 'Browsing Context' do | 
|  | 6 | +  let(:driver) do | 
|  | 7 | +    options = Selenium::WebDriver::Options.firefox | 
|  | 8 | +    options.add_option(:web_socket_url, true) | 
|  | 9 | +    Selenium::WebDriver.for :firefox, options: options | 
|  | 10 | +  end | 
|  | 11 | + | 
|  | 12 | +  after do | 
|  | 13 | +    driver.quit | 
|  | 14 | +  end | 
|  | 15 | + | 
|  | 16 | +  it 'test create a browsing context for given id' do | 
|  | 17 | +    id = driver.window_handle | 
|  | 18 | +    browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, browsing_context_id: id) | 
|  | 19 | +    expect(browsing_context.id).to eq(id) | 
|  | 20 | +  end | 
|  | 21 | + | 
|  | 22 | +  it 'test create a window' do | 
|  | 23 | +    browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :window) | 
|  | 24 | +    expect(browsing_context.id).not_to be_nil | 
|  | 25 | +  end | 
|  | 26 | + | 
|  | 27 | +  it 'test create a window with a reference context' do | 
|  | 28 | +    browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :window, | 
|  | 29 | +                                                                      reference_context: driver.window_handle) | 
|  | 30 | +    expect(browsing_context.id).not_to be_nil | 
|  | 31 | +  end | 
|  | 32 | + | 
|  | 33 | +  it 'test create a tab' do | 
|  | 34 | +    browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab) | 
|  | 35 | +    expect(browsing_context.id).not_to be_nil | 
|  | 36 | +  end | 
|  | 37 | + | 
|  | 38 | +  it 'test create a tab with a reference context' do | 
|  | 39 | +    browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab, | 
|  | 40 | +                                                                      reference_context: driver.window_handle) | 
|  | 41 | +    expect(browsing_context.id).not_to be_nil | 
|  | 42 | +  end | 
|  | 43 | + | 
|  | 44 | +  it 'test navigate to a url' do | 
|  | 45 | +    browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab) | 
|  | 46 | + | 
|  | 47 | +    info = browsing_context.navigate url: 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html' | 
|  | 48 | + | 
|  | 49 | +    expect(browsing_context.id).not_to be_nil | 
|  | 50 | +    expect(info.navigation_id).to be_nil | 
|  | 51 | +    expect(info.url).to include('/bidi/logEntryAdded.html') | 
|  | 52 | +  end | 
|  | 53 | + | 
|  | 54 | +  it 'test navigate to a url with readiness state' do | 
|  | 55 | +    browsing_context = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab) | 
|  | 56 | + | 
|  | 57 | +    info = browsing_context.navigate url: 'https://www.selenium.dev/selenium/web/bidi/logEntryAdded.html', | 
|  | 58 | +                                     readiness_state: :complete | 
|  | 59 | + | 
|  | 60 | +    expect(browsing_context.id).not_to be_nil | 
|  | 61 | +    expect(info.navigation_id).to be_nil | 
|  | 62 | +    expect(info.url).to include('/bidi/logEntryAdded.html') | 
|  | 63 | +  end | 
|  | 64 | + | 
|  | 65 | +  it 'test get tree with a child' do | 
|  | 66 | +    browsing_context_id = driver.window_handle | 
|  | 67 | +    parent_window = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, | 
|  | 68 | +                                                                   browsing_context_id: browsing_context_id) | 
|  | 69 | +    parent_window.navigate(url: 'https://www.selenium.dev/selenium/web/iframes.html', | 
|  | 70 | +                           readiness_state: :complete) | 
|  | 71 | + | 
|  | 72 | +    context_info = parent_window.get_tree | 
|  | 73 | +    expect(context_info.children.size).to eq(1) | 
|  | 74 | +    expect(context_info.id).to eq(browsing_context_id) | 
|  | 75 | +    expect(context_info.children[0]['url']).to include('formPage.html') | 
|  | 76 | +  end | 
|  | 77 | + | 
|  | 78 | +  it 'test get tree with depth' do | 
|  | 79 | +    browsing_context_id = driver.window_handle | 
|  | 80 | +    parent_window = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, | 
|  | 81 | +                                                                   browsing_context_id: browsing_context_id) | 
|  | 82 | +    parent_window.navigate(url: 'https://www.selenium.dev/selenium/web/iframes.html', | 
|  | 83 | +                           readiness_state: :complete) | 
|  | 84 | + | 
|  | 85 | +    context_info = parent_window.get_tree(max_depth: 0) | 
|  | 86 | +    expect(context_info.children).to be_nil | 
|  | 87 | +    expect(context_info.id).to eq(browsing_context_id) | 
|  | 88 | +  end | 
|  | 89 | + | 
|  | 90 | +  it 'test close a window' do | 
|  | 91 | +    window1 = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :window) | 
|  | 92 | +    window2 = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :window) | 
|  | 93 | + | 
|  | 94 | +    window2.close | 
|  | 95 | + | 
|  | 96 | +    expect { window1.get_tree }.not_to raise_error | 
|  | 97 | +    expect { window2.get_tree }.to raise_error(Selenium::WebDriver::Error::WebDriverError) | 
|  | 98 | +  end | 
|  | 99 | + | 
|  | 100 | +  it 'test close a tab' do | 
|  | 101 | +    tab1 = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab) | 
|  | 102 | +    tab2 = Selenium::WebDriver::BiDi::BrowsingContext.new(driver: driver, type: :tab) | 
|  | 103 | + | 
|  | 104 | +    tab2.close | 
|  | 105 | + | 
|  | 106 | +    expect { tab1.get_tree }.not_to raise_error | 
|  | 107 | +    expect { tab2.get_tree }.to raise_error(Selenium::WebDriver::Error::WebDriverError) | 
|  | 108 | +  end | 
|  | 109 | +end | 
0 commit comments