Forum Posts

alexsiminiuc3
Jun 21, 2018
In Selenium Questions
Use the following code to get the text of the selected option from a listbox: WebElement listElement = driver.findElement(elementId); Select list = new Select(listElement); WebElement selectedOption = list.getFirstSelectedOption(); String selectedText = selectedOption.getText();
0
0
26
alexsiminiuc3
Jun 14, 2018
In Selenium Questions
Have a look at this test: HomePage homepage; ResultsPage resultsPage; @AfterMethod public void after() { resultsPage.close(); } @BeforeMethod public void before() { homepage = new HomePage(); homepage.open(); assertTrue(homepage.isValidPage()); } @Test public void totalPriceTest() { homepage.typePostalCode(postalCode); homepage.selectRadius(radius); homepage.selectNearlyNew(selectNearlyNew); homepage.selectNew(selectNew); homepage.selectMake(make); homepage.selectModel(model); homepage.selectTotalPrice(); resultsPage = homepage.submitSearch(); assertTrue(resultsPage.isValidPage()); assertTrue(resultsPage.countContains(homepage.searchCount())); assertTrue(resultsPage.isRadiusSelected(radius)); assertTrue(resultsPage.isPostalCode(postalCode)); assertTrue(resultsPage.isMakeSelected(make)); assertTrue(resultsPage.isModelSelected(model)); assertTrue(resultsPage.carFiltersContain(homepage.selectedCarFilters())); resultsPage.selectTotalPriceLowest(); assertTrue(resultsPage.isSortOrderDescending(false)); } The test uses a home page that is opened in some way. In the homepage, multiple fields are populated and then a search is executed. The test then continues using a results page. The before() method creates the home page object, opens the home page and checks if it is displayed. The after() method closes the results page. The before() and after() methods works for a test that starts on the home page and ends on results page. But what if we have a test that starts on the home page and ends on details page? Or starts on the login page and ends on the checkout page? Because before() opens the home page, all tests must start with the home page. Because after() closes the results page, all tests must end on the results page. This is too restrictive and should be avoided by removing any page objects from both test fixture methods (before() and after()). Lets remember that before() and after() methods should just prepare the test environment so the tests can run in it and clear the test environment after the tests dont need it any longer. Opening a home page means starting to interact with the application. All interaction with the application should be in the test method. before() and after() are test fixtures. This is what they should do: A test fixture is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. Examples of fixtures: Preparation of input data and setup/creation of fake or mock objects; Loading a database with a specific, known set of data; Copying a specific known set of files creating a test fixture will create a set of objects initialized to certain states. from https://github.com/junit-team/junit4/wiki/test-fixtures
0
7
24
alexsiminiuc3
Jun 13, 2018
In Job Interview Tests
Assume that you want to select an option from the following listbox: <select name="make" id="searchVehiclesMake"> <option value="">Make (any)</option> <option value="a1">Abarth (699)</option> <option value="a2">AC (17)</option> <option value="a3">Aixam (1)</option> <option value="a4>Alfa Romeo (1,732)</option> <option value="a5">Alpine (1)</option> <option value="a6">Ariel (6)</option> <option value="a7">Aston Martin (791)</option> <option value="a8">Audi (30,707)</option> <option value="a9">Austin (29)</option> <option value="b1">Bac (3)</option> <option value="b2">Beauford (2)</option> <option value="b3">Bentley (1,014)</option> <option value="b4">BMW (36,612)</option> <option value="b5">Bowler (4)</option> <option value="b6">Brooke (1)</option> You want to select an option like BMW by visible text. Unfortunately, the following code does not work since the count of BMW vehicles changes: WebElement makeElement = driver.findElement(By.id("searchVehiclesMake")); Select makeList = new Select(makeElement); makeList.selectByVisibleText("BMW (36,612)"); Ideally, you would like to be able to do something like WebElement makeElement = driver.findElement(By.id("searchVehiclesMake")); Select makeList = new Select(makeElement); makeList.selectByVisibleText("BMW"); But selectByVisibleText() only works if you use the full visible text value for the option. Still, how can you select by the partial visible text?
0
1
90
alexsiminiuc3
Jun 10, 2018
In Job Interview Tests
This is a test from a real interview. A string S consisting of N characters is called properly nested if:  S is empty S has the form "(U)" where U is a properly nested string S has the form "VW" where V and W are properly nested strings For example, string "(()(())())" is properly nested but string "())" isn't.  Write a function int solution(char *S);  that,  given a string S consisting of N characters,  returns 1 if string S is properly nested and 0 otherwise.  For example, given S = "(()(())())", the function should return 1 and given S = "())", the function should return 0, as explained above.  Assume that the string S consists only of the characters "(" and/or ")".
0
0
20
alexsiminiuc3
Jun 10, 2018
In Job Interview Tests
this is a test from a real interview. 1. open https://ia.ca 2. click on Loans in the menu 3. click on Mortgages under Products 4. click on Mortgage Payment 5. move the Purchase Price Slider to the right 6. validate that the Purchase Price Slider movement works 7. change the Purchase Price to 500 000 using the + button of the slider 8. change the Down Payment to 100 000 using the + button of the slider 9. select 15 years for Amortization 10. select Weekly for Payment Frequency 11. change the Interest Rate to 5% 12. click the Calculate button 13. verify that the weekly payments value is 726.35
0
2
29
alexsiminiuc3
Jun 06, 2018
In Selenium Questions
Which test is better? The one without fluent methods? @Test public void canSelectResult() { HomePage homePage = new HomePage(); homePage.open(); ResultsPage resultsPage = homePage.searchFor("java"); DetailsPage detailsPage = resultsPage.selectFirstResult(); assertTrue(detailsPage.isDisplayed()); } or the one with fluent methods? @Test public void canSelectResult() { HomePage homePage = new HomePage(); DetailsPage detailsPage = homePage.open() .searchFor("java") .selectFirstResult(); assertTrue(detailsPage.isDisplayed()); } The test with fluent methods is worse. It seems simpler and shorter but it makes maintenance much more complicated. Let's see why. 1. You want to add a breakpoint on the searchFor() method for the following line: DetailsPage detailsPage = homePage.open() .searchFor("java") .selectFirstResult(); How do you do that? 2. You want to step through the code by starting at open(), stepping over it, stepping over searchFor() and stepping into selectFirstResult(): DetailsPage detailsPage = homePage.open() .searchFor("java") .selectFirstResult(); How do you do that with fluent methods? 3. An exception is thrown by the following line: DetailsPage detailsPage = homePage.open() .searchFor("java") .selectFirstResult(); Which method generated it? 4. Do you understand without looking at the method's code what is the return type of searchFor()? DetailsPage detailsPage = homePage.open() .searchFor("java") .selectFirstResult(); Try doing the same for HomePage homePage = new HomePage(); homePage.open(); ResultsPage resultsPage = homePage.searchFor("java"); DetailsPage detailsPage = resultsPage.selectFirstResult(); assertTrue(detailsPage.isDisplayed()); It is clear this time that searchFor() returns a ResultsPage. 5. When writing object oriented code, it should be clear the object that a method works on. Is it clear from the next line on which object selectFirstResult() works? DetailsPage detailsPage = homePage.open() .searchFor("java") .selectFirstResult();
0
0
8
alexsiminiuc3
Jun 06, 2018
In Job Interview Tests
Write a Java program that prints the numbers from 1 to 50. For multiples of 3, print "Fizz" instead of the number. For the multiples of 5, print "Buzz" instead of the number. For numbers which are multiples of both 3 and 5, print "FizzBuzz". For all other numbers, print the number. Good luck!
0
3
29
alexsiminiuc3
May 27, 2018
In Selenium Questions
If you include in the setUp() method code that opens the site, for example, @BeforeTest public void setUp() { System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe"); driver = new ChromeDriver(); driver.get(URL); searchForKeyword(keyword); } all test methods will have to start with opening the home page and running a keyword search. If one of the tests will prefer to start on a diferent page, this will not be possible. It is better to just set up the environment in the setUp() method and move all code that interacts with the site in each test method: @BeforeTest public void setUp() { System.setProperty("webdriver.chrome.driver", "c:/selenium/chromedriver.exe"); driver = new ChromeDriver(); }
0
0
8
alexsiminiuc3
May 26, 2018
In Selenium Questions
Test methods should not include any locators. Instead of having the following code in a test method selectListBox(SEARCH_MAKE_ID, make); selectListBox(SEARCH_MODEL_ID, model); selectListBox(SEARCH_MAXPRICE_ID, price); the test should have selectMake(CAR_MAKE); selectModel(CAR_MODEL); selectPrice(CAR_MAXPRICE); See below the code of all 4 methods: public void selectMake(String make) { selectListBox(SEARCH_MAKE_ID, make); } public void selectModel(String model) { selectListBox(SEARCH_MODEL_ID, model); } public void selectPrice(String price) { selectListBox(SEARCH_MAXPRICE_ID, price); } public void selectListBox(By listLocator, String listVisbleText) { WebElement listElement = wait.until(ExpectedConditions .visibilityOfElementLocated(listLocator)); Select list = new Select(listElement); list.selectByVisibleText(listVisbleText); }
0
0
6
alexsiminiuc3
May 26, 2018
In Selenium Questions
The test methods should be as simple as possible. Using java statements such as if/else, for, while, do-while, switch makes the test methods complicated. Instead of closing a random popup in the test method with if (isPopupDisplayed()==true) closePopup(); use a single method that does all work: closePopupIfDisplayed(); See below the code of the 3 methods: public Boolean isPopupDisplayed() { try { WebDriverWait shortWait = new WebDriverWait(driver, 5); WebElement popup = shortWait.until(ExpectedConditions .visibilityOfElementLocated(POPUP)); return true; } catch (Exception ex) { return false; } } public void closePopup() { WebElement element = wait.until(ExpectedConditions .elementToBeClickable(CLOSE_POPUP)); element.click(); } public void closePopupIfDisplayed() { if (isPopupDisplayed() closePopup(); }
0
0
6
alexsiminiuc3
May 26, 2018
In Selenium Questions
The first letter of each word of a variable name should be uppercase, with the exception of the first word. PostalBox should be postalBox. Same for the method names. CarSearchReturnsResults should be carSearchReturnsResults. The name of a variable or method should explain clearly the purpose of the variable or method (what is the variable about, what does the method do). A method that finds a text box and types a keyword in it should not be named sendPostalCode() but typePostalCode().
0
2
7
alexsiminiuc3
May 23, 2018
In Selenium Questions
When trying to find a locator for an element, go through the following steps: 1. if the element has id attribute with static value, locate the element by id 2. if the element has name attribute with static value, locate the element by name 3. if the element has other attributes with static values, try creating an xpath locator that identifies the element uniquely  4. if the element cannot be identified uniquely, find a parent of the element and then locate the element from the parent 
0
0
9
alexsiminiuc3
May 17, 2018
In Selenium Questions
If your project has multiple methods that either do similar things or depend on each other, they should be grouped together: public boolean isHomeDisplayed() { return urlContains("www.vpl.ca"); } public boolean isResultDisplayed() { return urlContains("vpl.bibliocommons.com"); } public boolean urlContains(String keyword) { return wait.until(ExpectedConditions.urlContains(keyword)); } First, you should have methods that do similar things like isHomeDisplayed() and isResultsDisplayed(). Then, since both these methods depend on the urlContains() method, urlContains() should be right after the first 2 methods. What is the reason? When reading the code of isHomeDisplayed, it is clear that it depends on urlContains. So you should look into urlContains next. If urlContains is after isHomeDisplayed, it is easy to see it. Otherwise, you have to locate it in the file which takes time.
0
3
14
alexsiminiuc3
May 16, 2018
In Selenium Questions
@Test public void searchResultBoardenCount() { int normalResultCount; openSite(); assertTrue(isHomeDisplayed() == true, "Home page is not displayed!"); searchFor(keyword); assertTrue(isResultDisplayed() == true, "Result page is not displayed!"); assertTrue(isCountDisplayed(), "Result Count not Displayed!"); assertTrue(count() > 0, "There are no results for the search!"); normalResultCount = count(); In this code sample, the normalResultCount variable is created at the beginning of the test method and used 10 lines below. When using it, if you will want to see what is the variable type, you will have to scroll up for this information. Scrolling up to get more information is a waste of time. Instead, you should just create the variable when you use it: @Test public void searchResultBoardenCount() { openSite(); assertTrue(isHomeDisplayed() == true, "Home page is not displayed!"); searchFor(keyword); assertTrue(isResultDisplayed() == true, "Result page is not displayed!"); assertTrue(isCountDisplayed(), "Result Count not Displayed!"); assertTrue(count() > 0, "There are no results for the search!"); int normalResultCount = count();
0
5
26
alexsiminiuc3
May 16, 2018
In Selenium Questions
public String getLocatorValues(By locator) { String values; WebElement locatorElement = wait.until(ExpectedConditions .visibilityOfElementLocated(locator)); values = locatorElement.getText(); return values; } This method uses a locator parameter. It finds an element using the locator, gets the element's text value and returns it. In short, it returns the text of an element. Then, it should not be called getLocatorValues but getElementText() or even shorter getText(). The locatorElement variable should be renamed as well to element: public String getText(By locator) { String value = ""; WebElement element = wait.until(ExpectedConditions .visibilityOfElementLocated(locator)); value = element.getText(); return value; } The name of a method should explain clearly what the method does. The name of a variable should explain clearly what the variable is. Both names should be as short as possible. One word is preferred. Two words is ok. Three words is bad.
0
2
21
alexsiminiuc3
May 16, 2018
In Selenium Questions
/* * Navigate to the site */ public void openSite() { driver.get(siteUrl); } /* * Method to check if the home page is displayed */ public boolean isHomeDisplayed() { return wait.until(ExpectedConditions.urlContains("vpl.ca")); } /* * Method check if the result page is displayed */ public boolean isResultDisplayed() { boolean isResultsPageDisplayed = urlContains("vpl.bibliocommons.com"); return isResultsPageDisplayed; } Each of the following 3 methods is very clear. They are all short. Their names are self-explanatory. The code is very simple. Adding a comment for each method that says what the method does is not needed. The method name already does this.
0
0
11
alexsiminiuc3
May 16, 2018
In Selenium Questions
For the login page of the VPL.ca site (https://vpl.bibliocommons.com/user/login?destination=%2Fuser_dashboard), the username, pin and login elements can be found not only by name but also by xpath. This is the HTML code of each element: USERNAME <input class="field_username text" data-js="username_login" id="user_name_2o8quogf7rlg8keurmdf7ra09p" name="name" testid="field_username" type="text"> PIN <input autocomplete="on" class="text" id="user_pin_2o8quogf7rlg8keurmdf7ra09p" name="user_pin" testid="field_userpin" type="password" value=""> LOGIN <input alt="Log In" class="btn btn-primary" data-js="button_login" name="commit" testid="button_login" title="Log In" type="submit" value="Log In"> All elements have a testid attribute with static values so possible xpath expressions are USERNAME //input[@testid = 'field_username'] PIN //input[@testid = 'field_userpin'] LOGIN //input[@testid = 'button_login'] Additional expressions can be created for each element: USERNAME //input[@class = 'field_username text'] //input[@data-js = 'username_login'] //input[contains(@id, 'user_name')] PIN //input[contains(@id, 'user_pin')] LOGIN //input[@data-js = 'button_login'] //input[@value = 'Log In'] Any expression can be used if it identifies the element uniquely. But locating by name is better than using an xpath selector.
0
8
64
alexsiminiuc3
May 14, 2018
In Selenium Questions
Add sufficient context to the question including - what you are trying to do - a code sample if needed - a screenshot - and explanations.
0
0
19
alexsiminiuc3
More actions