Can't close browser in Ionic 3 - ionic-framework

I need close InAppBrowser when some page starts load, my code:
constructor(private iab: InAppBrowser) {}
const options: InAppBrowserOptions = {
zoom: 'no',
hardwareback: 'no',
location: 'no'
};
const browser = this.iab.create('https://ionicframework.com/', '_self', options);
browser.on('loadstart').subscribe(event => {
if (event.url === 'https://ionicframework.com/getting-started') {
console.log('close!');
browser.close();
}
});
When /getting-started page starts load i see in Console 'close!', but browser won't close

Related

Ionic InAppBrowser and routes

I am working on an ionic project.
I need to use the InAppBrowser plugin to display a website.
But depending on which page I am, I need to change the view to an Ionic page.
export class HomePage {
constructor(public navCtrl: NavController, public inAppBrowser: InAppBrowser, public platform: Platform) {
var url = "https://helloworld.com";
var browser = inAppBrowser.create(url, '_blank', {
zoom: 'no',
location: 'no'
});
platform.ready().then(() => {
browser.show();
browser.on('loadstart').subscribe((e) => {
var navCtlr = this.navCtrl;
var scriptUrlParam = "window.addEventListener(\"click\",function(e){var href=e.target.getAttribute(\"href\");if(href){location.href=href+\"?source=mobapp\";e.preventDefault();}});";
var url = e.url;
if (url.indexOf("scanner") !== -1) {
alert("Page Scanner");
navCtrl.push(QrcodePage);
}
browser.insertCSS({
code: "#mobileMenu{display:block!important;}"
});
browser.executeScript({
code: scriptUrlParam
});
});
});
}
}
navCtrl.push(QrcodePage); is supposed to redirect me to the QR Code page. But it doesn't work.
I know I am on the right page because alert("Page Scanner"); is working.
Have anyone occurred this probleme already ?
Thanks

Ionic 3 The external links open only once in system browser outside in appbrowser

i wrapped a website and there are external links which i made to open in System browser, but when I return to in-app browser using hardware back button and again try to open the external links it doesn’t open. It looks like the 'loadstart" occurs only once.Or am i doing anything wrong here?
home.ts
export class HomePage {
constructor(public navCtrl: NavController, private iab: InAppBrowser, public
platform: Platform,private fileOpener: FileOpener, private
transfer:FileTransfer, private file: File, private diagnostic:Diagnostic) {
platform.ready().then(() => {
const browser =
this.iab.create('https://www.tutorialspoint.com/ionic/index.htm','_blank',
{zoom:'yes',location:'no', clearcache: 'yes', clearsessioncache: 'yes'});
browser.show();
browser.on('loadstart').subscribe(
(data) => {
console.log("URL IS", data.url);
this.downloadfile(data.url)
},
err => {
console.log("InAppBrowser Loadstop Event Error: " + err);
}
);
});
}
downloadfile(url) {
var externalCheck = (!url.includes("tutorial.points"));
var pdfCheck = (url.substr(url.length - 4) == '.pdf');
if (externalCheck || pdfCheck) {
window.open(url, "_system", 'location=no');
}
}
}
Is there any solution for this ? any help is really appreciated.Thanks in advance

Ionic DevApp: Google maps not showing

I have used JavaScript SDK to display map.
It's working on the browser but when I open it on dev app, the page shows the blank page.
ionViewDidLoad(){
this.loadMap();
}
loadMap(){
this.geolocation.getCurrentPosition().then((position) => {
let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
}, (err) => {
console.log(err);
});
}
Try to add locationOption
let locationOptions = { timeout: 30000, enableHighAccuracy: true };
and pass to getCurrentPosition like this
this.geolocation.getCurrentPosition(locationOptions).then((p‌​osition) => {

Launching Ionic app routing

Found this solution to lounching app via plugin
.run(['$state', '$window',
function($state, $window) {
$window.addEventListener('LaunchUrl', function(event) {
// gets page name from url
var page =/.*:[/]{2}([^?]*)[?]?(.*)/.exec(event.detail.url)[1];
// redirects to page specified in url
$state.go('tab.'+ page, {});
});
}]);
function handleOpenURL(url) {
setTimeout( function() {
var event = new CustomEvent('LaunchUrl', {detail: {'url': url}});
window.dispatchEvent(event);
}, 0);}
This solution works great if use simply state name, ex. myapp://posts
.state('app.posts', {
url: "/posts",
views: {
'menuContent': {
templateUrl: "templates/posts.html",
controller: 'PostsCtrl'
}
}})
open app/posts.
But I need open state like this
.state('app.post', {
url: "/posts/:postId",
views: {
'menuContent': {
templateUrl: "templates/post.html",
controller: 'PostCtrl'
}
}})
with link myapp://post/2525.
Can somebody help with this? Thank you!
Solved myself )))
.run(['$state', '$window',
function($state, $window) {
$window.addEventListener('LaunchUrl', function(event) {
// gets page name from url
var page =/.*:[/]{2}([^?]*)[?]?[/]{1}([^?]*)[?]?/.exec(event.detail.url)[1];
var id =/.*:[/]{2}([^?]*)[?]?[/]{1}([^?]*)[?]?/.exec(event.detail.url)[2];
// redirects to page specified in url
// alert ("id:" +id);
$state.go('app.'+ page, {'postId': + id});
});
}]);

protractor test timed out on Chrome but not on FF

I'm running a protractor test.
there is a login part which is not Angular and then the home page which is Angular part.
after passing the login part - on Chrome I get a Timeout, but on FF it continues successfully.
it used to run also on Chrome before, and a friend using Robot Framework doesn't get to timeout even when using chrome.
error message:
timeout: timed out after 30000 msec waiting for spec to complete
I've tries also adding
defaultTimeoutInterval: 360000
but it didn't help. it's just keep waiting.
config.js:
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
// 'browserName': 'firefox'
},
specs: ['*/loginEAPTest.js'],
params: {
login: {
user: '****',
password: '****'
}
},
resultJsonOutputFile: 'TestResults/Test.js',
jasmineNodeOpts: {
isVerbose: true,
showColors: true,
//defaultTimeoutInterval: 360000,
includeStackTrace: true
},
onPrepare: function() {
// for non-angular page
browser.ignoreSynchronization = true;
}
};
loginEAPTest.js:
var LoginPage = require('../global/LoginPage');
var capture = require('../global/screenshots');
describe('login Testing', function () {
var login = new LoginPage();
beforeEach(function(){
login.get();
login.justClickLogin();
}
);
it('Should login using Email & Password', function () {
browser.sleep(10000);
browser.driver.findElement(by.id('userName_str')).sendKeys ("****");
browser.driver.findElement(by.id('password')).sendKeys("******");
browser.driver.findElement(by.xpath("//input[contains(#value,'Log In')]")).click();
browser.ignoreSynchronization = false;
browser.sleep(5000);
browser.switchTo().defaultContent();
expect(element(by.css('[data-ui-sref=myApps]')).isDisplayed()).toBe(true);
});
});
LoginPage.js (relevant part):
this.justClickLogin = function() {
var loginObj = this;
loginObj.signInButton.click().then(function () {
console.log("press login");
var loginframe = element(by.tagName('iframe'));
console.log("before switch");
browser.switchTo().frame(0);
console.log("after switch");
browser.sleep(4000);
});
};
thanks.