Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.DriverHelper.getCurrentUrl
Source:DriverHelper.java
...625 public boolean isUrlAsExpected(String expectedURL) {626 String decryptedURL = cryptoTool.decryptByPattern(expectedURL, CRYPTO_PATTERN);627 decryptedURL = decryptedURL.startsWith("http") ? decryptedURL : Configuration.get(Parameter.URL) + decryptedURL;628 WebDriver drv = getDriver();629 if (LogicUtils.isURLEqual(decryptedURL, drv.getCurrentUrl())) {630 Messager.EXPECTED_URL.info(drv.getCurrentUrl());631 return true;632 } else {633 Messager.UNEXPECTED_URL.error(expectedURL, drv.getCurrentUrl());634 return false;635 }636 }637 /**638 * Pause for specified timeout.639 * 640 * @param timeout641 * in seconds.642 */643 public void pause(long timeout) {644 CommonUtils.pause(timeout);645 }646 public void pause(Double timeout) {647 CommonUtils.pause(timeout);648 }649 /**650 * Checks that page title is as expected.651 * 652 * @param expectedTitle653 * Expected title654 * @return validation result.655 */656 public boolean isTitleAsExpected(final String expectedTitle) {657 boolean result;658 final String decryptedExpectedTitle = cryptoTool.decryptByPattern(expectedTitle, CRYPTO_PATTERN);659 final WebDriver drv = getDriver();660 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);661 try {662 wait.until((Function<WebDriver, Object>) dr -> drv.getTitle().contains(decryptedExpectedTitle));663 result = true;664 Messager.TITLE_CORERECT.info(drv.getCurrentUrl(), expectedTitle);665 } catch (Exception e) {666 result = false;667 Messager.TITLE_NOT_CORERECT.error(drv.getCurrentUrl(), expectedTitle, drv.getTitle());668 }669 return result;670 }671 /**672 * Checks that page suites to expected pattern.673 * 674 * @param expectedPattern675 * Expected Pattern676 * @return validation result.677 */678 public boolean isTitleAsExpectedPattern(String expectedPattern) {679 boolean result;680 final String decryptedExpectedPattern = cryptoTool.decryptByPattern(expectedPattern, CRYPTO_PATTERN);681 WebDriver drv = getDriver();682 String actual = drv.getTitle();683 Pattern p = Pattern.compile(decryptedExpectedPattern);684 Matcher m = p.matcher(actual);685 if (m.find()) {686 Messager.TITLE_CORERECT.info(drv.getCurrentUrl(), actual);687 result = true;688 } else {689 Messager.TITLE_DOES_NOT_MATCH_TO_PATTERN.error(drv.getCurrentUrl(), expectedPattern, actual);690 result = false;691 }692 return result;693 }694 /**695 * Go back in browser.696 */697 public void navigateBack() {698 getDriver().navigate().back();699 Messager.BACK.info();700 }701 /**702 * Refresh browser.703 */704 public void refresh() {705 getDriver().navigate().refresh();706 Messager.REFRESH.info();707 }708 /**709 * Refresh browser after timeout.710 * 711 * @param timeout712 * before refresh.713 */714 public void refresh(long timeout) {715 CommonUtils.pause(timeout);716 refresh();717 }718 /**719 * Selects text in specified select element.720 *721 * @param extendedWebElement722 * Element723 * @param selectText724 * select text725 * @return true if item selected, otherwise false.726 */727 @Deprecated728 public boolean select(final ExtendedWebElement extendedWebElement, final String selectText) {729 return extendedWebElement.select(selectText);730 }731 /**732 * Select multiple text values in specified select element.733 * 734 * @param extendedWebElement final ExtendedWebElement735 * @param values final String[]736 * 737 * @return boolean738 */739 @Deprecated740 public boolean select(final ExtendedWebElement extendedWebElement, final String[] values) {741 return extendedWebElement.select(values);742 }743 @Deprecated744 public void select(String controlInfo, WebElement control, String selectText) {745 select(new ExtendedWebElement(control, controlInfo, getDriver()), selectText);746 }747 /**748 * Selects value according to text value matcher.749 *750 * @param extendedWebElement751 * Element752 * @param matcher753 * {@link} BaseMatcher754 * @return true if item selected, otherwise false.755 */756 @Deprecated757 public boolean selectByMatcher(final ExtendedWebElement extendedWebElement, final BaseMatcher<String> matcher) {758 return extendedWebElement.selectByMatcher(matcher);759 }760 /**761 * Selects first value according to partial text value.762 * 763 * @param extendedWebElement764 * Element765 * @param partialSelectText766 * select by partial text767 * @return true if item selected, otherwise false.768 */769 @Deprecated770 public boolean selectByPartialText(final ExtendedWebElement extendedWebElement, final String partialSelectText) {771 return extendedWebElement.selectByPartialText(partialSelectText);772 }773 /**774 * Selects item by index in specified select element.775 * 776 * @param extendedWebElement final ExtendedWebElement777 * @param index to select by778 * 779 * @return true if item selected, otherwise false.780 */781 @Deprecated782 public boolean select(final ExtendedWebElement extendedWebElement, final int index) {783 return extendedWebElement.select(index);784 }785 @Deprecated786 public void select(String controlInfo, WebElement control, int index) {787 select(new ExtendedWebElement(control, controlInfo, getDriver()), index);788 }789 /**790 * Hovers over element.791 * 792 * @param extendedWebElement793 * final ExtendedWebElement794 *795 */796 @Deprecated797 public void hover(final ExtendedWebElement extendedWebElement) {798 hover(extendedWebElement, null, null);799 }800 @Deprecated801 public void hover(final ExtendedWebElement extendedWebElement, Integer xOffset, Integer yOffset) {802 extendedWebElement.hover(xOffset, yOffset);803 }804 @Deprecated805 public void hover(String controlInfo, WebElement control) {806 hover(new ExtendedWebElement(control, controlInfo, getDriver()));807 }808 public void pressTab() {809 Actions builder = new Actions(getDriver());810 builder.sendKeys(Keys.TAB).perform();811 }812 @Deprecated813 public void sendKeys(String keys) {814 final String decryptedKeys = cryptoTool.decryptByPattern(keys, CRYPTO_PATTERN);815 Actions builder = new Actions(getDriver());816 builder.sendKeys(decryptedKeys).perform();817 }818 /**819 * Close alert modal by JS.820 */821 @Deprecated822 public void silentAlert() {823 WebDriver drv = getDriver();824 ((JavascriptExecutor) drv).executeScript("window.alert = function(msg) { return true; }");825 ((JavascriptExecutor) drv).executeScript("window.confirm = function(msg) { return true; }");826 ((JavascriptExecutor) drv).executeScript("window.prompt = function(msg) { return true; }");827 }828 /**829 * Drags and drops element to specified place.830 * 831 * @param from832 * - element to drag.833 * @param to834 * - element to drop to.835 */836 public void dragAndDrop(final ExtendedWebElement from, final ExtendedWebElement to) {837 if (isElementPresent(from) && isElementPresent(to)) {838 WebDriver drv = getDriver();839 if (!drv.toString().contains("safari")) {840 Actions builder = new Actions(drv);841 Action dragAndDrop = builder.clickAndHold(from.getElement()).moveToElement(to.getElement())842 .release(to.getElement()).build();843 dragAndDrop.perform();844 } else {845 WebElement LocatorFrom = from.getElement();846 WebElement LocatorTo = to.getElement();847 String xto = Integer.toString(LocatorTo.getLocation().x);848 String yto = Integer.toString(LocatorTo.getLocation().y);849 ((JavascriptExecutor) driver)850 .executeScript(851 "function simulate(f,c,d,e){var b,a=null;for(b in eventMatchers)if(eventMatchers[b].test(c)){a=b;break}if(!a)return!1;document.createEvent?(b=document.createEvent(a),a==\"HTMLEvents\"?b.initEvent(c,!0,!0):b.initMouseEvent(c,!0,!0,document.defaultView,0,d,e,d,e,!1,!1,!1,!1,0,null),f.dispatchEvent(b)):(a=document.createEventObject(),a.detail=0,a.screenX=d,a.screenY=e,a.clientX=d,a.clientY=e,a.ctrlKey=!1,a.altKey=!1,a.shiftKey=!1,a.metaKey=!1,a.button=1,f.fireEvent(\"on\"+c,a));return!0} var eventMatchers={HTMLEvents:/^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,MouseEvents:/^(?:click|dblclick|mouse(?:down|up|over|move|out))$/}; "852 + "simulate(arguments[0],\"mousedown\",0,0); simulate(arguments[0],\"mousemove\",arguments[1],arguments[2]); simulate(arguments[0],\"mouseup\",arguments[1],arguments[2]); ",853 LocatorFrom, xto, yto);854 }855 Messager.ELEMENTS_DRAGGED_AND_DROPPED.info(from.getName(), to.getName());856 } else {857 Messager.ELEMENTS_NOT_DRAGGED_AND_DROPPED.error(from.getNameWithLocator(), to.getNameWithLocator());858 }859 }860 /**861 * Performs slider move for specified offset.862 * 863 * @param slider864 * slider865 * @param moveX866 * move x867 * @param moveY868 * move y869 */870 public void slide(ExtendedWebElement slider, int moveX, int moveY) {871 //TODO: SZ migrate to FluentWaits872 if (isElementPresent(slider)) {873 WebDriver drv = getDriver();874 (new Actions(drv)).moveToElement(slider.getElement()).dragAndDropBy(slider.getElement(), moveX, moveY)875 .build().perform();876 Messager.SLIDER_MOVED.info(slider.getNameWithLocator(), String.valueOf(moveX), String.valueOf(moveY));877 } else {878 Messager.SLIDER_NOT_MOVED.error(slider.getNameWithLocator(), String.valueOf(moveX), String.valueOf(moveY));879 }880 }881 /**882 * Get selected elements from one-value select.883 * 884 * @param select885 * Element886 * @return selected value887 */888 @Deprecated889 public String getSelectedValue(ExtendedWebElement select) {890 return select.getSelectedValue();891 }892 /**893 * Get selected elements from multi-value select.894 * 895 * @param select896 * Element897 * @return selected value898 */899 @Deprecated900 public List<String> getSelectedValues(ExtendedWebElement select) {901 return select.getSelectedValues();902 }903 /**904 * Accepts alert modal.905 */906 public void acceptAlert() {907 WebDriver drv = getDriver();908 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);909 try {910 wait.until((Function<WebDriver, Object>) dr -> isAlertPresent());911 drv.switchTo().alert().accept();912 Messager.ALERT_ACCEPTED.info("");913 } catch (Exception e) {914 Messager.ALERT_NOT_ACCEPTED.error("");915 }916 }917 /**918 * Cancels alert modal.919 */920 public void cancelAlert() {921 WebDriver drv = getDriver();922 wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);923 try {924 wait.until((Function<WebDriver, Object>) dr -> isAlertPresent());925 drv.switchTo().alert().dismiss();926 Messager.ALERT_CANCELED.info("");927 } catch (Exception e) {928 Messager.ALERT_NOT_CANCELED.error("");929 }930 }931 /**932 * Checks that alert modal is shown.933 * 934 * @return whether the alert modal present.935 */936 public boolean isAlertPresent() {937 try {938 getDriver().switchTo().alert();939 return true;940 } catch (NoAlertPresentException Ex) {941 return false;942 }943 }944 // --------------------------------------------------------------------------945 // Methods from v1.0946 // --------------------------------------------------------------------------947 public boolean isPageOpened(final AbstractPage page) {948 return isPageOpened(page, EXPLICIT_TIMEOUT);949 }950 public boolean isPageOpened(final AbstractPage page, long timeout) {951 boolean result;952 final WebDriver drv = getDriver();953 wait = new WebDriverWait(drv, timeout, RETRY_TIME);954 try {955 wait.until((Function<WebDriver, Object>) dr -> LogicUtils.isURLEqual(page.getPageURL(), drv.getCurrentUrl()));956 result = true;957 } catch (Exception e) {958 result = false;959 }960 if (!result) {961 LOGGER.warn(String.format("Actual URL differs from expected one. Expected '%s' but found '%s'",962 page.getPageURL(), drv.getCurrentUrl()));963 }964 return result;965 }966 /**967 * Executes a script on an element968 * 969 * Really should only be used when the web driver is sucking at exposing970 * functionality natively971 * 972 * @param script973 * The script to execute974 * @param element975 * The target of the script, referenced as arguments[0]976 */...
getCurrentUrl
Using AI Code Generation
1public String getCurrentUrl() {2 return getDriver().getCurrentUrl();3}4public void get(String url) {5 getDriver().get(url);6}7public void navigate(String url) {8 getDriver().navigate().to(url);9}10public void navigateBack() {11 getDriver().navigate().back();12}13public void navigateForward() {14 getDriver().navigate().forward();15}16public void refresh() {17 getDriver().navigate().refresh();18}19public String getTitle() {20 return getDriver().getTitle();21}22public String getPageSource() {23 return getDriver().getPageSource();24}25public String getWindowHandle() {26 return getDriver().getWindowHandle();27}28public Set<String> getWindowHandles() {29 return getDriver().getWindowHandles();30}31public void switchToWindow(String windowName) {32 getDriver().switchTo().window(windowName);33}34public void switchToFrame(String frameName) {35 getDriver().switchTo().frame(frameName);36}37public void switchToFrame(int frameIndex) {38 getDriver().switchTo().frame(frameIndex);39}40public void switchToFrame(WebElement frame
getCurrentUrl
Using AI Code Generation
1import org.testng.Assert;2import org.testng.annotations.Test;3import com.qaprosoft.carina.core.foundation.utils.ownership.MethodOwner;4import com.qaprosoft.carina.demo.gui.components.HeaderMenu;5public class GetCurrentUrlTest extends AbstractTest {6 @MethodOwner(owner = "qpsdemo")7 public void testGetCurrentUrl() {8 HeaderMenu headerMenu = new HeaderMenu(getDriver());9 String currentUrl = headerMenu.getCurrentUrl();10 }11}
getCurrentUrl
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.DriverHelper;2import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.support.FindBy;5public class TestPage extends AbstractPage {6 private ExtendedWebElement link;7 public TestPage(WebDriver driver) {8 super(driver);9 }10 public void clickLink() {11 link.click();12 }13 public void printCurrentUrl() {14 System.out.println(DriverHelper.getCurrentUrl());15 }16}17Sometimes, there is a need to use WebDriver methods directly. To do this, we can use the getDriver() method of the DriverHelper class. Let’s add a new test to the TestPageTest class:18public void testClickLinkGetDriver() {19 TestPage testPage = new TestPage(getDriver());20 testPage.open();21 testPage.clickLink();22 System.out.println(testPage.getDriver().getCurrentUrl());23}24public void testClickLinkGetDriverPageObject() {25 TestPage testPage = new TestPage(getDriver());26 testPage.open();27 testPage.clickLink();28 System.out.println(testPage.getCurrentUrl());29}
getCurrentUrl
Using AI Code Generation
1public class TestClass extends AbstractTest {2 public void test() {3 String currentUrl = DriverHelper.getCurrentUrl();4 Assert.assertTrue(currentUrl.contains("google"));5 }6}7public class TestClass extends AbstractTest {8 public void test() {9 String currentUrl = DriverHelper.getCurrentUrl();10 Assert.assertTrue(currentUrl.contains("google"));11 }12}13public class TestClass extends AbstractTest {14 public void test() {15 String currentUrl = DriverHelper.getCurrentUrl();16 Assert.assertTrue(currentUrl.contains("google"));17 }18}19public class TestClass extends AbstractTest {20 public void test() {21 String currentUrl = DriverHelper.getCurrentUrl();22 Assert.assertTrue(currentUrl.contains("google"));23 }24}25public class TestClass extends AbstractTest {26 public void test() {27 String currentUrl = DriverHelper.getCurrentUrl();28 Assert.assertTrue(currentUrl.contains("google"));29 }30}31public class TestClass extends AbstractTest {32 public void test() {33 String currentUrl = DriverHelper.getCurrentUrl();34 Assert.assertTrue(currentUrl.contains("google"));35 }36}37public class TestClass extends AbstractTest {38 public void test() {39 String currentUrl = DriverHelper.getCurrentUrl();40 Assert.assertTrue(currentUrl.contains("google"));41 }42}43public class TestClass extends AbstractTest {44 public void test() {45 String currentUrl = DriverHelper.getCurrentUrl();46 Assert.assertTrue(currentUrl.contains("google"));47 }48}49public class TestClass extends AbstractTest {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!