Wednesday, October 5, 2016

Selenium Challenges

Challenges faced using selenium automation testing, and how to solve them 1. Dealing with pop-up windows: Selenium can sometimes fail to record common popups in web apps. To handle any kind of alert popup, you can apply a getAlert function. Before actually running the script, you must import a package that can generate a WebDriver script for handling alerts. The efficient interface brings with it the following commands: void dismiss(), void accept (), getText(), void sendKeys(String stringToSend). The first two basically click on the “cancel” and “OK” buttons respectively on a popup window. 2. No event trigger from value changes: Because Selenium does not initiate events with a change in values, one must do it oneself using fireEvent: selenium.FireEvent(cmbCategory, “onchange”); 3. Timeout resulting from synchronization problems: One should ideally use selenium.IsElementPresent(locator) to verify that the object is in a loop with Thread.Sleep 4. Protected Mode must be set to the same value error occurs when trying to run Selenium WebDriver on a fresh Windows machine. This issue can be fixed by using capabilities as below when launching IE DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); WebDriver driver = new InternetExplorerDriver(caps); 1) Some elements like text box, buttons etc. are taking more time(more than given Implicit wait time) to appear on page of software web application or to get enabled on page We can use Explicit wait to handle this situation 2) Handling dynamic changing ID to locate element Is tricky. If element's ID Is changing every time when you reload the software web application page and you have to use that ID In XPath to locate element then you have to use functions like starts-with(@id,'post-body-') or contains(@id,'post-body-') In XPath 3) Clicking on sub menus which are getting rendered on mouse hover of main menu is somewhat tricky. You need to use webdriver's Actions class to perform mouse hover operation. Actions actions = new Actions(driver); WebElement moveonmenu = driver.findElement(By.xpath("//div[@id='menu1']/div")); actions.moveToElement(moveonmenu); actions.perform(); 4) If you have to execute your test cases In multiple browsers then one test case can run successfully In Firefox browser but same test case may fail In IE browser due to the timing related Issues (nosuchelement exception) because test execution In Firefox browser Is faster than IE browser. You can resolve this Issue by Increasing Implicit wait time when you run your test In IE browser. Elements not found on page ---------------------------- Can happen due to execution speed of Selenium Elements do not load on page in time Fix : Add code in scripts which waits for elements Additional issues: Sometimes existing elements on a page are not detected Sometimes elements on a page have a same label Solution: Obtain exact element label from source code Label can be used in scripts for accuracy Handling of Popup windows ---------------------------- Window id required for automated operations in window At times if id is not obtained during recording, go to window and perform operations Window id will be obtained after this action. Handling of Frames -------------------- Option: -multiWindow option handles potential frame problems Causes : At times required name of frame not obtained in recording Fix: Obtain frame label from source code Also can be obtained from browser URL bar