webdriver (c#) - looping page refresh until an elements style doesn't exist - c#

I'm struggling a little with a method I need. Here is what I want to do:
I want to load a webpage
On that webpage, I want to wait until a specific element and it's style attribute contains "hidden: true;". When this style is present, the page has been fully loaded so I can continue my tests
I need to continue refreshing the webpage until the above condition is true - Please assume I need to keep refreshing. I know it seems stupid.
Here is what I'm trying
Step 1:
Navigating to a webpage (writing code from memory):
driver.Navigate().GoToUrl("http://example.com");
Step 2:
Find the style from the specific element (Writing code from memory)
Iwebelement MyElement = driver.findElement(By.id("example"));
Iwebelement MyElementStyle = MyElement.GetAttribute("style");
Step 3:
If the element has a style of "hidden: true;" on page load then do nothing else perform a page refresh until the element doesn't contain a style of "hidden: true;" (Writing code from memory)
if(MyElementStyle.Contains("hidden: true;"))
{
// do nothing
}
else
{
driver.Navigate().GoToUrl("http://example.com");
}
This appears to be working but the above code will only refresh the page one time so I need to loop rather than use an if statement.
Would anyone be able to offer a few pointers or suggest a better way of approaching this?

I am not sure what language binding you are using but looks like a mixture of C# and java and if that's the real test code it will not work.
Use do while loop to do a post check and check if the attribute has expected value or not.
IWebElement MyElement = Driver.FindElement(By.Id("example"));
string MyElementStyle = MyElement.GetAttribute("style");
do
{
Driver.Navigate().Refresh();
} while (MyElementStyle.Contains("hidden: true;"));

Related

C# Selenium can't find element with WebDriverWait, i can see and click on it manually

Element im trying to locate:
WebDriverWait wait = new WebDriverWait(browser, TimeSpan.FromSeconds(60));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//use[#xlink:href='#core_mail']")));
File.WriteAllText("html.txt", browser.PageSource);
that just times out.. and the page loads way before 60 seconds.
svg has a default namespace. You have to account for it:
//svg:use[#xlink:href='#core_mail']
Or ignore it with local-name():
//*[local-name() = 'use' and #xlink:href='#core_mail']
Though, to be fair, you don't have to dive into the markup that deeply, your "email" button is much higher in the tree - see the very first parent element partially visible on the screenshot - that's your desired element you probably want to locate instead.

How to initialize WebElements in Selenium page object

I am new to Selenium and looking forward to learn more, I am using Selenium WebDriver with C#.
This is how I initiate a WebElement:
CarouselSliderNextButton = DriverInitializer.driver
.FindElement(By.XPath("//a[#class='buttons next']"));
But if the element doesn't exist for some reason; hidden for instance, then it doesn't work. I know that I can check if the element exists before I can initiate, but would love to hear from experts if I am doing this the right way.
I am not claiming to be an expert, but this is what I think:
You do not "initiate" a web element
You find a web element
First of all, you need to find the element you want to locate from html file. You may use Google or Firefox in developer mode to find it. I recommend you to install "Firebug" for Firefox, it is very useful.
There are multiple reasons why an element you can "see" from HTML file but you can not locate:
1: this element is on an iframe, this case requires you to locate this iframe first then locate the element
2: this element is not visible yet, for example, a drop down arrow button will only appear if you hover your mouse over a certain area first.
But you are heading to the right direction.
If you're trying to intialise the WebElement, I use:
WebElement element = driver.findElement(By.tagName("div"));
as most DOMs have div tags.
Then after trying to find an element that exists (and isn't a div tag), check:
if (element.getTagName().equals("div")){
System.out.println("Element not found...");
}
var instantEstimateDiv: WebElement? = null => Kotlin
WebElement? instantEstimateDiv= null; => JAVA

Div visibility in javascript

i was working on some requirements of my project and one of the requirement was to hide/unhide a Div on client side(Project is in .net technology and div's visibility will be set on client side using JS)
Code Snippet:
var block = document.getElementById('Your_Div_Id');
block.style.display = "none"; //some where it works to hide
block.style.visibility = "hidden"; //some where it works to hide
my question is why?
This is just hit and trial. first one was not working in one place so I used second one.I could not got to know why...
In case you dont want to use jQuery, Make sure your Id of element is unique and is set properly in javascript code
you may also try writing the code in one line as below :
document.getElementById("element-id").style.display="none";
Remember that display: none and visibility: hidden are different.
The first one "delete" the node from the DOM, and the other nodes can take it place.
The second one just hide hide, but the node preserves it position and sizes.
Tip: Try to use jQuery
$("#foo").hide();

JS/C# how do I know the elment id or xpath I just clicked or manipulated?

As you know, Selenium IDE can show you a script and tell you what element you just manipulated.
I don't know the what happened behind this. Every automation framework can know what element you are clicked, inputed, or selected..
I want to have a console output and display every element I just manipulated in browser.
How can I implement this?
Using jQuery:
$("*").click(function(e) {
console.log(this);
}.blur(function(e) {
console.log(this);
};
Just pop this on your page somewhere and it'll log the element that was manipulated whenever it's clicked or selected. I'm sure there are other ways of manipulation, but the above is a start. It would be trivial to add more handlers--.change() and .hover() come to mind.

Test if an element is focused using Selenium Webdriver

I'm really surprised I can't find references on the internet to testing for element focus using Selenium Webdriver.
I'm wanting to check when when a form submission is attempted with a mandatory field missed, focus is moved to the empty field. But I cannot see any way to do this using the WebDriver API.
I will be able to find the focused element using a JavascriptExecutor. But reading the FAQ makes me think there must be some way to perform the check using the driver itself.
Thanks for any help.
driver.switchTo().activeElement() will return the currently focused WebElement. Equality is well defined for WebElement, so you can call element.equals(driver.switchTo().activeElement()).
Calling the slightly misleading named driver.switchTo().activeElement() does not in fact switch focus, neither does driver.findElement(), so you do not need to switchTo().defaultContent() after; in fact, doing so would probably blur the current element.
driver.switchTo().activeElement();
returns the currently focused element.
Makes sure you switch back after using
driver.switchTo().defaultContent();
Also if nothing is focused the body of the document is returned.
Take a look at this question as well.
In Selenium how do I find the "Current" object
You can find the active element using selector 'dom=document.activeElement'. Then you can assert whether it's the element you want it to be focused or not.
The WebDriver is supposed to change focus when you use Driver.FindElement calls. So you're last element in the Driver context is active.
NOTE: This breaks for any elements injected dynamic (e.g. jQuery), so you'd need to go the script route then.
#danielwagner-hall The boolean bb = driver.switchTo().activeElement().equals(driver.findElement(By.id("widget_113_si‌​gnup_username"))); will always pass but it doesn't prove that the element is brought into focus if the element is out of view.
NB: Unable to comment as not enough reputation points.
One way of approaching this could be to use webElement.getLocation().getX(); getY() methods and reference the coordinates on the page and verify its focus.

Categories

Resources