How to switch to a window (NOT TAB) in karate? [duplicate] - karate

I am having an issue with switching tabs using Karate UI. My code is as follows:
Scenario: SwitchPage Test
Given driver 'original URL'
* retry(5, 10000).waitFor('#someID')
* input('#someID', ['numbers', 'input', Key.ENTER]) // this will open the new page
* print driver.title \\ this prints the original title of the original URL
* switchPage('NewURL')
* delay(10000) // I've put this just in case but it doesn't work without it either
Then print driver.title // this still prints the original title of the original URL
Any help would be really appreciated, I think it is a great tool but I'm having difficulty with this scenario and our application opens new tabs with every module.
Thank you

Yes I know this can be hard. In 0.9.6 we have an option to switch tab by a) index and b) sub-string of the URL of that tab
Do consider submitting a simple example that can help us simulate and improve Karate if needed: https://github.com/intuit/karate/tree/develop/examples/ui-test
Finally I recommend this option if possible. If there is some way for you to derive the URL of the tab that will get opened, try to re-use the current tab, and that makes for a simpler test. See this thread for more details: https://github.com/intuit/karate/issues/1269#issuecomment-682553602
Also see: https://stackoverflow.com/a/62727612/143475

Related

Swithcpage or new tab is not working in karate framework - it is always pointing to first page [duplicate]

I am having an issue with switching tabs using Karate UI. My code is as follows:
Scenario: SwitchPage Test
Given driver 'original URL'
* retry(5, 10000).waitFor('#someID')
* input('#someID', ['numbers', 'input', Key.ENTER]) // this will open the new page
* print driver.title \\ this prints the original title of the original URL
* switchPage('NewURL')
* delay(10000) // I've put this just in case but it doesn't work without it either
Then print driver.title // this still prints the original title of the original URL
Any help would be really appreciated, I think it is a great tool but I'm having difficulty with this scenario and our application opens new tabs with every module.
Thank you
Yes I know this can be hard. In 0.9.6 we have an option to switch tab by a) index and b) sub-string of the URL of that tab
Do consider submitting a simple example that can help us simulate and improve Karate if needed: https://github.com/intuit/karate/tree/develop/examples/ui-test
Finally I recommend this option if possible. If there is some way for you to derive the URL of the tab that will get opened, try to re-use the current tab, and that makes for a simpler test. See this thread for more details: https://github.com/intuit/karate/issues/1269#issuecomment-682553602
Also see: https://stackoverflow.com/a/62727612/143475

Karate UI automation, is it possible to make locators dynamic

Thanks Peter for API,Perf karate framework.
Now I am trying UI automation and it looks perfect as of now.
I have below question:
I have a menu bar with 15 items in it. they are named as following:
-Home
-Account
-groups
-settings
etc etc.
Locator for each one is like this "a[name=Home]" , "a[name=Account]", "a[name=groups]" . So ony the part after name= is something that is dynamic in nature. So my question is that is it possible to somehow make this dynamic locator?
I have written a called feature in which i have written steps to login and select a menu item. and I want to pass the menu item from calling feature in json like below:
if I want to click on Account menu
* call read(menuItem.feature) {menuItem: Account}
menuItem.feature looks something like below:
Given url 'xyz'
And input (username, userID)
And input (password, password)
And click("button[name=login]")
And click("a[name=Home]")
Here I want to make Home as dynamic in last click step on menuItem.feature so that I can pass other menu items and reuse all above steps/feature file to login everytime with different menu items passed from calling feature file in json{.
Again its just a query. I have currently written multiple scenarios to click on menu item and then in calling feature I call the called feature with tags like #Home,#account etc. and this works perfectly fine.
Also if there is any other better way to achieve this then please suggest
Yes. Let me put it this way, the things you see such as input('foo', 'bar') is pure JS behind the scenes. So normal programming-language rules apply. For example:
* def dynamic = 'foo'
* def locator = 'a[name=' + dynamic + ']'
* input(locator, 'some value')

Selenium driver is not reflecting updates after click on link

There are some posts about this topic but I cannot find any solution for my case, this is the situation:
I click on a link (a next page):
ActionChains(driver).move_to_element(next_el).click().perform()
Then I get the content of the new page(I'm interested on some script sections inside the body)
html = driver.find_element_by_xpath("//*").get_attribute("outerHTML")
But that content is always the same, no matter how long I wait for.
The only way to get the driver with new DOM information is to do a refresh(),
but for this case that is not a valid option.
Thanks and regards.
I am not sure what exactly you are looking for here, but if I am right you want to capture the content of script tag from the page.
If that is the case capture the page source in a string variable
sorce_code = driver.page_source , after you get the sting you can extract the value by any of the available string methods. I hope it helps.

Click a Link working in Selenium Ide but not working in Webdriver

I am testing a scenario for the site "https://www.freecrm.com/index.html"
login credentials [ john2013 / john2013 ]
Scenario :
1 open the site https://www.freecrm.com/index.html
2 login with valid credentials
3 click on the "New Contacts" link
4 Add new contacts
using Selenium ide i am able to login and click on the "New Contact" link , but when i am trying to do the same thing using Webdriver [ java] i am not able to click the "New Contact" link
the code i have written is given below
driver.findElement(By.name("username")).clear()
driver.findElement(By.name("username")).sendKeys("john2013");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("john2013");
driver.findElement(By.cssSelector("input[type=\"image\"]")).click();
The code up to the above is working fine but clicking the "New Contact" link
driver.findElement(By.xpath("//div[#class='noprint']/span[#class='headertext']/a[3]/")).click();
is not working though the same xpath is working in IDE.
i have tried with expected condition option , sleep but nothing is working.
can any one help me in this regard.
The problem is that your page uses a frameset. You should tell your webdriver which frame to use when it will search for your element. So try this:
driver.findElement(By.name("username")).clear()
driver.findElement(By.name("username")).sendKeys("john2013");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("john2013");
driver.findElement(By.cssSelector("input[type=\"image\"]")).click();
//switch the driver to use one of the frames on your page. Potentially wait for a bit till the page is loaded
driver.switchTo().frame("mainpanel");
driver.findElement(By.xpath("//*[text()='New Contact']")).click();
Please change the xpath of link to
xpath=//a[contains(text(),'<whatevertext>')]
Incase if the same text link is already present in some part o the page we can proceed with occurences. Example if it is repeating second time:
xpath=(//a[contains(text(),'<whatevertext>')])[2]
If Nothing is working the best way is through java script executor.
First get the webelement of the link.
Then use the following code"
webelement=driver.findElement(by.xpath("=//a[contains(text(),'')]
"))
(JavaScriptExecutor)driver.executescript("argument[0].click();",webelement)
Actually Speaking //div[#class='noprint']/span[#class='headertext']/a[3]/ is absolute XPath.
as it ends with a[3] the position will vary always so it is advisable to use relative XPath/CSS
Suggested CSS:css=.noprint > .headertext > a.classname
Another Suggested CSS: css=.noprint > .headertext > a[attribute='value']
So we can use either of the above format.

Finding text on page with Selenium 2

How can I check whether a given text string is present on the current page using Selenium?
The code is this:
def elem = driver.findElement(By.xpath("//*[contains(.,'search_text')]"));
if (elem == null) println("The text is not found on the page!");
If your searching the whole page for some text , then providing an xpath or selector to find an element is not necessary. The following code might help..
Assert.assertEquals(driver.getPageSource().contains("text_to_search"), true);
For some reason, certain elements don't seem to respond to the "generic" search listed in the other answer. At least not in Selenium2library under Robot Framework which is where I needed this incantation to find the particular element:
xpath=//script[contains(#src, 'super-sekret-url.example.com')]
A simpler (but probably less efficient) alternative to XPaths is to just get all the visible text in the page body like so:
def pageText = browser.findElement(By.tagName("body")).getText();
Then if you're using JUnit or something, you can use an assertion to check that the string you are searching for is contained in it.
assertThat("Text not found on page", pageText, containsString(searchText));
Using an XPath is perhaps more efficient, but this way is simpler to understand for those unfamiliar with it. Also, an AssertionError generated by assertThat will include the text that does exist on the page, which may be desirable for debugging as anybody looking at the logs can clearly see what text is on the page if what we are looking for isn't.