|
| 1 | +/* |
| 2 | + * Copyright 2002-2007 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.suren.autotest.web.framework.selenium; |
| 18 | + |
| 19 | +import org.openqa.selenium.Alert; |
| 20 | +import org.openqa.selenium.UnhandledAlertException; |
| 21 | +import org.openqa.selenium.WebDriver; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | + |
| 24 | +import com.surenpi.autotest.webui.Page; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author suren |
| 28 | + * @date 2017年7月4日 下午7:51:57 |
| 29 | + */ |
| 30 | +public class WebPage extends Page |
| 31 | +{ |
| 32 | + @Autowired |
| 33 | + private SeleniumEngine engine; |
| 34 | + |
| 35 | + @Override |
| 36 | + public void open() |
| 37 | + { |
| 38 | + String paramUrl = paramTranslate(getUrl()); |
| 39 | + |
| 40 | + engine.openUrl(paramUrl); |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + engine.computeToolbarHeight(); |
| 45 | + } |
| 46 | + catch(UnhandledAlertException e) |
| 47 | + { |
| 48 | + Alert alert = engine.getDriver().switchTo().alert(); |
| 49 | + if(alert != null) |
| 50 | + { |
| 51 | + alert.dismiss(); |
| 52 | + |
| 53 | + engine.computeToolbarHeight(); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void close() |
| 60 | + { |
| 61 | + engine.close(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void closeOthers() |
| 66 | + { |
| 67 | + String currentTitle = getTitle(); |
| 68 | + String currentWinHandle = engine.getDriver().getWindowHandle(); |
| 69 | + |
| 70 | + for(String winHandle : engine.getDriver().getWindowHandles()) |
| 71 | + { |
| 72 | + WebDriver itemDrive = engine.getDriver().switchTo().window(winHandle); |
| 73 | + if(!itemDrive.getTitle().equals(currentTitle)) |
| 74 | + { |
| 75 | + itemDrive.close(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + engine.getDriver().switchTo().window(currentWinHandle); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String getCurrentUrl() |
| 84 | + { |
| 85 | + return engine.getDriver().getCurrentUrl(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public String getPageSource() |
| 90 | + { |
| 91 | + return engine.getDriver().getPageSource(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public String getTitle() |
| 96 | + { |
| 97 | + return engine.getDriver().getTitle(); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments