Cucumber + Capybara tests to ensure a new window is opened - ruby-on-rails-3

I have the following lines in my feature file:
Given I have website "www.google.co.uk"
When I click the website "www.google.co.uk"
Then "www.google.co.uk" page is opened in a new window
I am struggling to find a way to test that the webpage is definately opened in a new window.
Currently I have been using this in my step def:
Then /url "([^"]*)" is opened in new window/ do |url|
browser = page.driver.browser
current_id = browser.window_handle
tab_id = page.driver.find_window(url)
browser.switch_to.window tab_id
page.driver.browser.close
browser.switch_to.window current_id
end
but this still passes the test if the webpage is loaded on the same page, I want it to fail if the webpage is loaded on the same window/tab.
Any suggestions?
Many thanks

I see no assertions in your test.
My approach would be to test size of window_handles array after performing clicking action on the link, since before clicking the size should equal 1 and after the clicking window_handles should equal 2.
assert page.driver.browser.window_handles.size == 2
Imho, good enough, since if the webpage is loaded in the same tab, the size will be 1 and the test will fail.

Related

Can we get to know Browser rendering time for each page in JMeter using webDriver sampler?

I am planning to do a load test with around 220 users ,Client is expecting Browser rendering time as well. So I though 1 will create one script for load test ,and create one more script with Selenium script in JMeter to measure rendering time. So that while executing load test , if I execute selenium script as well. It will give the Browser rendering time.
But as I saw, With Selenium sampler ,Aggregate report shows end to end response time. If i want to know the Browser rendering time of each page ,if there any way to get the breakdown?
You have 2 options:
Use a separate WebDriver Sampler per "page" like:
Alternatively you can use WDS.sampleResult.addSubResult function to add "child" results to a single WebDriver Sampler instance, example code would be something like:
WDS.sampleResult.sampleStart()
var seleniumDev = new org.apache.jmeter.samplers.SampleResult()
seleniumDev.setSampleLabel('Selenium main page')
seleniumDev.sampleStart()
WDS.browser.get('https://selenium.dev')
seleniumDev.setResponseCodeOK()
seleniumDev.setSuccessful(true)
seleniumDev.sampleEnd()
WDS.sampleResult.addSubResult(seleniumDev)
var jmeter = new org.apache.jmeter.samplers.SampleResult()
jmeter.setSampleLabel('JMeter main page')
jmeter.sampleStart()
WDS.browser.get('https://jmeter.apache.org')
jmeter.setResponseCodeOK()
jmeter.setSuccessful(true)
jmeter.sampleEnd()
WDS.sampleResult.addSubResult(jmeter)
WDS.sampleResult.sampleEnd()
resulting in the following:
More information: The WebDriver Sampler: Your Top 10 Questions Answered

Threading and Selenium

I'm trying to make multiple tabs in Selenium and open a page on each tab simultaneously. Here is the code.
CHROME_DRIVER_PATH = "C:/chromedriver.exe"
from selenium import webdriver
import threading
driver = webdriver.Chrome(CHROME_DRIVER_PATH)
links = ["https://www.google.com/",
"https://stackoverflow.com/",
"https://www.reddit.com/",
"https://edition.cnn.com/"]
def open_page(url, tab_index):
driver.switch_to_window(handles[tab_index])
driver.get(url)
return
# open a blank tab for every link in the list
for link in range(len(links)-1 ): # 1 less because first tab is already opened
driver.execute_script("window.open();")
handles = driver.window_handles # get handles
all_threads = []
for i in range(0, len(links)):
current_thread = threading.Thread(target=open_page, args=(links[i], i,))
all_threads.append(current_thread)
current_thread.start()
for thr in all_threads:
thr.join()
Execution goes without errors, and from what I understand this should logically work correctly. But, the effect of the program is not as I imagined. It only opens one page at a time, sometimes it doesn't even switch the tab... Is there a problem that I'm not aware of in my code or threading doesn't work with Selenium?
There is no need in switching to new window to get URL, you can try below to open each URL in new tab one by one:
links = ["https://www.google.com/",
"https://stackoverflow.com/",
"https://www.reddit.com/",
"https://edition.cnn.com/"]
# Open all URLs in new tabs
for link in links:
driver.execute_script("window.open('{}');".format(link))
# Closing main (empty) tab
driver.close()
Now you can handle (if you want) all the windows from driver.window_handles as usual

The selenium got the different html source from my local pc

I fetched http://book.flypeach.com/default.aspx?ao=B2CZHTW&ori=TPE&des=KIX&dep=2015-06-12-undefined-undefined&adt=1&chd=0&inf=0&langculture=zh-TW&bLFF=false by driver.current_url
However I got timeout error by this code
wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
wait.until { #driver.find_element(:css => "div.WrapperFlightDate") }
But I can see the css attributes div.WrapperFlightDate was truly exsiting in the page_source,
How could it happen ?
When I opened given link and trying to see given class in source code(ctrl + u),I didn't find given class.May be page was not loaded properly.
I refreshed page and right click on page -> View Page Source option then got this class in source code.Even I also ran this successfully in FF.
I also surprised why I didn't see correct source code first time.
You also try same,hope so you also get correct code now :).

Selenium's WebDriver.execute_script() returns 'None'

My program is having trouble getting an existing class from a webpage using Selenium. It seems that my WebDriver.execute_script function is not working.
import urllib
from selenium import webdriver
#Path to the chromedriver is definitely working fine.
path_to_chromedriver = 'C:\Users\Ben\Desktop\Coding\FreeFoodFinder\chromedriver.exe'
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://www.maidservicetexas.com/'
browser.implicitly_wait(30)
browser.get(url)
content = browser.execute_script("document.getElementsByClassName('content')");
#Just printing the first character of the returned content's toString for now. Don't want the whole thing yet.
#Only ever prints 'N', the first letter of 'None'...so obviously it isn't finding the jsgenerated content even after waiting.
print content
My program returns 'None,' which tells me that the javascript function is not returning a value/being executed. Chrome's web dev tools tell me that 'content' is certainly a valid class name. The webpage isn't even dynamically generated (my eventual goal is to scrape dynamic content, which is why I make my WebDriver wait for 30 seconds before running the script.)
Return the value:
content = browser.execute_script("return document.getElementsByClassName('content');");

Can't find a popup window opened via target="_blank"

I am working with a website that has a form that submits a POST request and opens the target page in a new window using target="_blank". Firefox opens it in a new window.
When I do a loop like
String titles[];
do {
titles = browser.getAllWindowTitles();
System.out.println(titles.length);
} while (titles.length < 2);
It always prints out 1, even when the new window pops up and loads. How can I select this new window?
I am using DefaultSelenium. I have tried approaching it in other, indirect ways such as direct URL loading, but the site sends POST requests and I'm unable to simulate those.
Try the below alternative code and check if it is working as expected.
Set titles = browser.getAllWindowTitles();
Iterator i = titles.iterator();
while(i.hasNext()){
System.out.println(titles.size);
}